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 a[4050][4050];
int n,m,dis[4050][4050],xx[]={1,-1,0,0},yy[]={0,0,1,-1},ans;
int main()
{
cin>>n>>m;
for(int i=0; i<n; i++)cin>>a[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)dis[i][j]=1e9;
}
deque<pair<int,int> >dq;
dq.push_front({0,0});
dis[0][0]=1;
while(dq.size())
{
pair<int,int>pr=dq.front();
dq.pop_front();
int ox=pr.first,oy=pr.second;
for(int i=0; i<4; i++)
{
int x=ox+xx[i],y=oy+yy[i];
if(0<=x&&x<n&&0<=y&&y<m&&a[x][y]!='.')
{
int pls=0;
if(a[ox][oy]!=a[x][y])pls++;
if(dis[x][y]>dis[ox][oy]+pls)
{
dis[x][y]=dis[ox][oy]+pls;
if(pls)dq.push_back({x,y});
else dq.push_front({x,y});
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)ans=max(ans,dis[i][j]);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |