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
//#define endl "\n"
const int mod = (int) 1e9 + 7;
const int N=4005;
int n,m,ans,nm;
char c,tmp;
char v[N][N];
int cst[N][N];
bool vis[N][N];
bool bl;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
bool chk(int i,int j) {
return i>=0 && j>=0 && i<n && j<m && !vis[i][j] && v[i][j]!='.';
}
void bfs() {
deque<pair<int,int> > q; q.push_front({0,0});
while (!q.empty()) {
int i=q.front().first;
int j=q.front().second;
q.pop_front();
vis[i][j]=true;
ans=max(ans,cst[i][j]);
for (int p=0;p<4;p++) {
int ii=i+dx[p],jj=j+dy[p];
if (chk(ii,jj)) {
if (v[i][j]==v[ii][jj]) {cst[ii][jj]=cst[i][j]; q.push_front({ii,jj});}
else {cst[ii][jj]=cst[i][j]+1;q.push_back({ii,jj});}
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin); freopen(".out", "w", stdout);
cin>>n>>m;
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
cin>>v[i][j];
}
}
cst[0][0]=1; bfs();
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |