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 N 4003
#define NN 16000002
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
int n, m, ans, cnt, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int dist[N][N];
char mat[N][N], T;
vector<int> grafo[NN];
int bfs01()
{
deque < pii > bfs;
bfs.push_back({1, 1});
memset(dist, 0x3f3f3f3f, sizeof dist);
dist[1][1] = 1;
while(!bfs.empty())
{
int x = bfs.front().f, y = bfs.front().s;
bfs.pop_front();
ans = max(ans, dist[x][y]);
for(int p = 0; p < 4; p++)
{
int a = x + dx[p], b = y + dy[p];
if(!a or !b or a > n or b > m or mat[a][b] == '.') continue;
int custo = (mat[a][b] == mat[x][y] ? 0 : 1);
if(dist[a][b] > dist[x][y] + custo)
{
dist[a][b] = dist[x][y] + custo;
if(!custo) bfs.push_front({a, b});
else bfs.push_back({a, b});
}
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) cin>>mat[i][j];
cout<<bfs01()<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |