#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int hashp = uniform_int_distribution<int>(10, 1e9)(rng);
int hashm1 = 1e9+7;
int hashm2 = 1e9+9;
int MOD = 1e9+9;
int MV = 1e18;
void solve(){
int h, w;
cin >> h >> w;
vector<vector<char>> grid(h, vector<char>(w));
vector<vector<int>> d(h, vector<int>(w, 0));
deque<array<int, 2>> dq;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> grid[i][j];
}
}
d[0][0] = 1;
dq.pb({0, 0});
int res = 1;
while(!dq.empty()){
auto[x, y] = dq.front();
dq.pop_front();
res = max(res, d[x][y]);
for(auto[dx, dy]: dir){
int nx = x+dx;
int ny = y+dy;
if(nx >= 0 && nx < h && ny >= 0 && ny < w && d[nx][ny] == 0 && grid[nx][ny] != '.'){
if(grid[nx][ny] == grid[x][y]){
d[nx][ny] = d[x][y];
dq.push_front({nx, ny});
} else{
d[nx][ny] = d[x][y]+1;
dq.pb({nx, ny});
}
}
}
}
cout << res;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |