#include <bits/stdc++.h>
using namespace std;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m, depth[4000][4000], ans = 1;
string snow[4000];
bool inside(int x, int y) {
return (x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.');
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> snow[i];
deque<pair<int, int>> q;
q.push_back({0, 0});
depth[0][0] = 1;
while (q.size()) {
auto [curx, cury] = q.front();
q.pop_front();
ans = max(ans, depth[curx][cury]);
for (int i = 0; i < 4; i++) {
int x = curx + dx[i], y = cury + dy[i];
if (inside(x, y) && depth[x][y] == 0) {
if (snow[x][y] == snow[curx][cury]) {
depth[x][y] = depth[curx][cury];
q.push_front({x, y});
} else {
depth[x][y] = depth[curx][cury] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |