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>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=4e3+5;
char a[N][N];
int d[N][N];
int h,w;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
cin>>h>>w;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>a[i][j];
}
}
vector <int> x={1,-1,0,0},y={0,0,1,-1};
deque <pair <int,int> > q;
q.push_back({0,0});
d[0][0]=1;
while(!q.empty()){
pair <int,int> v=q.front();
q.pop_front();
int X=v.ff,Y=v.ss;
for(int i=0;i<4;i++){
if(X+x[i]>=0 && X+x[i]<h && Y+y[i]>=0 && Y+y[i]<w && a[X+x[i]][Y+y[i]]!='.' && d[X+x[i]][Y+y[i]]==0){
if(a[X+x[i]][Y+y[i]]==a[X][Y]){
d[X+x[i]][Y+y[i]]=d[X][Y];
q.push_front({X+x[i],Y+y[i]});
}
else{
d[X+x[i]][Y+y[i]]=d[X][Y]+1;
q.push_back({X+x[i],Y+y[i]});
}
}
}
}
int ans=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
ans=max(ans,d[i][j]);
}
}
cout<<ans<<"\n";
}
/*
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |