#include <bits/stdc++.h>
#include <utility>
using namespace std;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1}, ans = 1;
int n, m;
int depth[4000][4000];
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(make_pair(0, 0));
depth[0][0] = 1;
while (q.size()) {
pair<int, int> curr = q.front();
q.pop_front();
ans = max(ans, depth[curr.first][curr.second]);
for (int i = 0; i < 4; i++) {
int x = curr.first + dx[i], y = curr.second + dy[i];
if (inside(x, y) && depth[x][y] == 0) {
if (snow[x][y] == snow[curr.first][curr.second]) {
depth[x][y] = depth[curr.first][curr.second];
q.push_front(make_pair(x, y));
} else {
depth[x][y] = depth[curr.first][curr.second] + 1;
q.push_back(make_pair(x, y));
}
}
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |