#include <bits/stdc++.h>
using namespace std;
const int N=4005;
const int dy[]={-1,1,0,0};
const int dx[]={0,0,-1,1};
int n,m,d[N][N],ans;
char c[N][N];
deque <pair <int,int>> q;
bool inside(int y,int x){
if(y<1 || y>n || x<1 || x>m) return false;
else return true;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin >> c[i][j];
d[1][1]=1;
q.push_back({1,1});
while(!q.empty()){
auto [y,x]=q.front();
q.pop_front();
ans=max(ans,d[y][x]);
for(int i=0;i<4;i++){
int yy=y+dy[i],xx=x+dx[i];
if(!inside(yy,xx) || d[yy][xx]!=0 || c[yy][xx]=='.') continue;
if(c[yy][xx]==c[y][x]){
d[yy][xx]=d[y][x];
q.push_front({yy,xx});
}
else{
d[yy][xx]=d[y][x]+1;
q.push_back({yy,xx});
}
}
}
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |