이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int mxN = 4000, INF = 2e9;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int H, W, depth[mxN][mxN];
string grid[mxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> H >> W;
for (int i=0; i<H; ++i)
cin >> grid[i];
deque<pair<int, int>> dq;
dq.push_front({0, 0});
depth[0][0] = 1;
int ans = 0;
while (!dq.empty()) {
int r = dq.front().first;
int c = dq.front().second;
dq.pop_front();
ans = max(ans, depth[r][c]);
for (int i=0; i<4; ++i) {
int nr = r + dx[i], nc = c + dy[i];
if (nr >= 0 && nr < H && nc >= 0 && nc < W &&
grid[nr][nc] != '.' && depth[nr][nc] == 0) {
if (grid[r][c] == grid[nr][nc]) {
depth[nr][nc] = depth[r][c];
dq.push_front({nr, nc});
} else {
depth[nr][nc] = depth[r][c] + 1;
dq.push_back({nr, nc});
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |