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 ll long long
#define mod 998244353
using namespace std;
int h,w;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int depth[4000][4000], ans = 1;
string grid[4000];
bool isfree(int x, int y)
{
if(x>=0 && x<h && y>=0 && y<w && grid[x][y]!='.')
{
return true;
}
return false;
}
int main()
{
iostream::sync_with_stdio(false);
cin.tie(0);
cin>>h>>w;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
cin>>grid[i][j];
}
}
deque<pair<int,int>> q;
q.push_back({0,0});
depth[0][0]=1;
while(q.size())
{
pair<int,int> cur=q.front();
q.pop_front();
ans=max(ans, depth[cur.first][cur.second]);
for(int i=0; i<4; i++)
{
int x=cur.first+dx[i];
int y=cur.second+dy[i];
if(isfree(x,y) && depth[x][y]==0)
{
if(grid[x][y]==grid[cur.first][cur.second])
{
depth[x][y]=depth[cur.first][cur.second];
q.push_front({x,y});
}
else
{
depth[x][y]=depth[cur.first][cur.second]+1;
q.push_back({x,y});
}
}
}
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |