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;
char g[4005][4005];
int ans[4005][4005];
int dx[] = {0,1,0,-1}, dy[] = {1, 0, -1, 0};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int h,w,maxi = 0;
cin >> h >> w;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin >> g[i][j];
}
}
for(int i=1;i<=4000;i++)fill(ans[i] , ans[i] + 4005, 2e9 +5);
queue<pair<int, int> >q;
q.push(make_pair(1, 1));
ans[1][1] = 0;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
if(x == h && y == w)continue;
for(int i=0;i<4;i++){
int x1 = x + dx[i];
int y1 = y + dy[i];
if(g[x1][y1] == '.')continue;
int ans1 = ans[x][y];
if(g[x1][y1] != g[x][y])ans1++;
if(ans[x1][y1] > ans1){
ans[x1][y1] = ans1;
q.push(make_pair(x1, y1));
}
}
}
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(g[i][j] == '.')continue;
maxi = max(maxi, ans[i][j]);
}
}
cout << maxi + 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |