This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |