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;
#define int long long
#define MAXN 4002
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m, depth[MAXN][MAXN], ans = 1;
char snow[MAXN][MAXN];
bool inside(int x, int y) {
return (x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.');
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>snow[i][j];
}
}
memset(depth,0,sizeof(depth));
deque<pair<int,int>>dq;
dq.push_back({0,0});
depth[0][0]=1;
while(!dq.empty()){
auto curr = dq.front();
int r = curr.first, c = curr.second;
dq.pop_front();
ans = max(ans,depth[r][c]);
for(int i=0;i<4;i++){
int nr = r+dx[i], nc = c+dy[i];
if(inside(nr,nc)&&depth[nr][nc]==0){
if(snow[r][c]==snow[nr][nc]){
depth[nr][nc] = depth[r][c];
dq.push_front({nr,nc});
}
else{
depth[nr][nc] = depth[r][c]+1;
dq.push_back({nr,nc});
}
}
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |