이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4000 + 10;
int h[maxn][maxn];
string s[maxn];
int adjx[] = {0, -1, 0, 1};
int adjy[] = {1, 0, -1, 0};
int main(){
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> s[i];
deque<pair<int,int>> deQ;
memset(h, -1, sizeof h);
h[0][0] = 1;
deQ.push_back({0,0});
int answer = 0;
while (!deQ.empty()){
auto it = deQ.front();
deQ.pop_front();
int x = it.first, y = it.second;
answer = max(answer, h[x][y]);
for (int z = 0; z < 4; z++){
int nx = x + adjx[z], ny = y + adjy[z];
if (0 <= nx and nx < n and 0 <= ny and ny < m and s[nx][ny] != '.'){
int w = (s[nx][ny] != s[x][y]);
if (h[nx][ny] != -1 and h[nx][ny] <= h[x][y] + 1)
continue;
h[nx][ny] = h[x][y] + w;
if (w == 0)
deQ.push_front({nx,ny});
else
deQ.push_back({nx,ny});
}
}
}
cout << answer << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |