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;
int n , m , dx[4] = {1,0,-1,0} , dy[4] = {0,1,0,-1} , dist[4005][4005] , vis[4005][4005] , ans=1;
char grid[4005][4005];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for(int i=0 ; i<n ; i++)for(int j=0 ; j<m ; j++)cin >> grid[i][j];
deque<pair<int,int>>q;
q.push_front({0,0});
vis[0][0] = 1;
dist[0][0] = 1;
while(q.size())
{
int x = q.front().first , y = q.front().second;
q.pop_front();
ans = max(ans , dist[x][y]);
for(int k=0 ; k<4 ; k++)
{
int xx = x + dx[k] , yy = y + dy[k];
if(xx<0 || yy<0 || xx>=n || yy>=m || grid[xx][yy] == '.' || vis[xx][yy])continue;
if(grid[x][y] == grid[xx][yy])
{
q.push_front({xx , yy});
dist[xx][yy] = dist[x][y];
}
else
{
q.push_back({xx , yy});
dist[xx][yy] = dist[x][y] + 1;
}
vis[xx][yy] = 1;
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |