This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dx[4]={0,0,1,-1}, dy[4]={1,-1,0,0};
int n,m,depth[4000][4000], ans=1;
string grid[4000];
bool check(int x, int y){
return (x>-1&&x<n&&y>-1&&y<m&&grid[x][y]!='.');
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=0;i<n;i++)
cin >> grid[i];
deque<pair<int,int>> q;
q.push_back({0,0});
depth[0][0]=1;
while(!q.empty()){
pair<int,int> t=q.front();
q.pop_front();
ans=max(ans,depth[t.first][t.second]);
for(int i=0;i<4;i++){
int x=t.first+dx[i], y=t.second+dy[i];
if(check(x,y)&&depth[x][y]==0){
if(grid[x][y]==grid[t.first][t.second]){
depth[x][y]=depth[t.first][t.second];
q.push_front({x,y});
}
else{
depth[x][y]=depth[t.first][t.second]+1;
q.push_back({x,y});
}
}
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |