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;
int visited[4001][4001];
char arr[4001][4001];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
bool canGo(int x, int y)
{
return (x <= n && y <= m && min(x, y) > 0 && arr[x][y] != '.' && !visited[x][y]);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> arr[i][j];
}
}
visited[1][1] = 1;
deque<pair<int, int>> q;
q.emplace_front(1, 1);
int ans = 1;
while (!q.empty())
{
int ux, uy;
tie(ux, uy) = q.front();
q.pop_front();
ans = max(ans, visited[ux][uy]);
for (int i = 0; i < 4; i++)
{
int vx = ux + dx[i];
int vy = uy + dy[i];
if (!canGo(vx, vy)) continue;
if (arr[vx][vy] == arr[ux][uy])
{
visited[vx][vy] = visited[ux][uy];
q.emplace_front(vx, vy);
}
else
{
visited[vx][vy] = visited[ux][uy] + 1;
q.emplace_back(vx, vy);
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |