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 ll long long
#define ar array
const int mxN=4000, di[4]={1, -1, 0, 0}, dj[4]={0, 0, 1, -1};
int n, m, d[mxN][mxN], ans;
string g[mxN];
bool vis[mxN][mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
cin >> g[i];
memset(d, 0x3f, sizeof(d));
d[0][0]=1;
deque<ar<int, 2>> q={{0, 0}};
while(q.size()) {
int i=q.front()[0], j=q.front()[1];
q.pop_front();
if (vis[i][j])
continue;
vis[i][j]=1;
ans=max(ans, d[i][j]);
for (int k=0; k<4; ++k) {
int ni=i+di[k], nj=j+dj[k];
if (0<=ni&&ni<n&&0<=nj&&nj<m&&!vis[ni][nj]&&g[ni][nj]^'.') {
if (g[i][j]==g[ni][nj]&&d[i][j]<d[ni][nj]) {
d[ni][nj]=d[i][j];
q.push_front({ni, nj});
} else if (g[i][j]^g[ni][nj]&&d[i][j]+1<d[ni][nj]) {
d[ni][nj]=d[i][j]+1;
q.push_back({ni, nj});
}
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |