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 pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int h,w;
cin>>h>>w;
string s[h+5];
int used[h+5][w+5];
for(int i=0;i<h;i++){
cin>>s[i];
for(int j=0;j<w;j++){
used[i][j]=0;
}
}
used[0][0]=1;
deque<pair<int,int>>q;
q.pb({0,0});
int ans=0;
while(!q.empty()){
int x=q.front().ff,y=q.front().ss;
ans=max(ans,used[x][y]);
q.pop_front();
if(x>0&&used[x-1][y]==0&&s[x-1][y]!='.'){
if(s[x-1][y]==s[x][y]){
used[x-1][y]=used[x][y];
q.push_front({x-1,y});
}
else{
used[x-1][y]=used[x][y]+1;
q.pb({x-1,y});
}
}
if(x<h-1&&used[x+1][y]==0&&s[x+1][y]!='.'){
if(s[x+1][y]==s[x][y]){
used[x+1][y]=used[x][y];
q.push_front({x+1,y});
}
else{
used[x+1][y]=used[x][y]+1;
q.pb({x+1,y});
}
}
if(y>0&&used[x][y-1]==0&&s[x][y-1]!='.'){
if(s[x][y-1]==s[x][y]){
used[x][y-1]=used[x][y];
q.push_front({x,y-1});
}
else{
used[x][y-1]=used[x][y]+1;
q.pb({x,y-1});
}
}
if(y<w-1&&used[x][y+1]==0&&s[x][y+1]!='.'){
if(s[x][y+1]==s[x][y]){
used[x][y+1]=used[x][y];
q.push_front({x,y+1});
}
else{
used[x][y+1]=used[x][y]+1;
q.pb({x,y+1});
}
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |