#include <bits/stdc++.h>
using namespace std;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
bool inside(int x, int y, int h, int w) {
return 0<=x&&x<h&&0<=y&&y<w;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector<string> a(h);
for(string& s: a) cin >> s;
vector<vector<int>> depth(h, vector<int>(w));
int ans = 1;
deque<pair<int, int>> dq;
dq.push_back({0, 0});
depth[0][0] = 1;
while(dq.size()) {
auto c = dq.front(); dq.pop_front();
ans = max(ans, depth[c.first][c.second]);
for(int i=0; i<4; i++){
int x = c.first+dx[i]; int y = c.second + dy[i];
if(inside(x, y, h, w) && a[x][y]!='.'&&depth[x][y]==0) {
if(a[x][y]==a[c.first][c.second]) {
depth[x][y] = depth[c.first][c.second];
dq.push_front({x, y});
} else {
depth[x][y] = depth[c.first][c.second]+1;
dq.push_back({x, y});
}
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |