#include<bits/stdc++.h>
using namespace std;
using i64 = long long int;
void solve() {
int n, m;
cin >> n >> m;
vector<string> snow(n);
for(int i = 0; i < n; i++) cin >> snow[i];
vector<vector<int>> depth(n + 1, vector<int>(m + 1, 0));
depth[0][0] = 1;
vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
deque<pair<int, int>> q;
q.push_back({0, 0});
auto inside = [&](int x, int y) -> bool {
return (x >= 0 && x < n && y >= 0 && y < m && snow[x][y] != '.');
};
int ans = 1;
while(!q.empty()) {
pair<int, int> u = q.front();
q.pop_front();
ans = max(ans, depth[u.first][u.second]);
for(int i = 0; i < 4; i++) {
int x = u.first + dx[i];
int y = u.second + dy[i];
if(inside(x, y) && !depth[x][y]) {
if(snow[u.first][u.second] == snow[x][y]) {
depth[x][y] = depth[u.first][u.second];
q.push_front({x, y});
} else {
depth[x][y] = depth[u.first][u.second] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |