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 int long long
int n, m, depth[4001][4001];
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
string a[4001];
bool inside(int i, int j)
{
if(i<0 or j<0 or i>=n or j>=m or a[i][j]=='.') return false;
return true;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
depth[0][0]=1;
deque<pair<int,int>> Q;
Q.push_back({0,0}); int ans = 0;
while(!Q.empty())
{
auto s = Q.front(); Q.pop_front();
int x = s.first, y = s.second;
ans = max(ans, depth[x][y]);
for(int i = 0; i < 4; i++)
{
int nx = x+X[i], ny = y+Y[i];
if(inside(nx,ny) and depth[nx][ny]==0){
if(a[x][y]==a[nx][ny]){
depth[nx][ny]=depth[x][y];
Q.push_front({nx,ny});
}
else{
depth[nx][ny]=1+depth[x][y];
Q.push_back({nx,ny});
}
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |