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 ll long long
#define pii pair<int, int>
#define endl '\n'
using namespace std;
int N, M;
vector<vector<bool>> vis;
vector<vector<char>> grid;
vector<vector<int>> depth;
int mvt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool is_ok(int r, int c)
{
return r >= 0 && c >= 0 && r < N && c < M && grid[r][c] != '.' && !vis[r][c];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
vis.resize(N, vector<bool>(M, false));
grid.resize(N, vector<char>(M));
depth.resize(N, vector<int>(M));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> grid[i][j];
deque<pii> q;
q.push_back({0, 0});
depth[0][0] = 1;
vis[0][0] = true;
int res = 0;
while (!q.empty())
{
int r, c;
tie(r, c) = q.front();
q.pop_front();
res = max(res, depth[r][c]);
for (int i = 0; i < 4; i++)
{
int rx = r + mvt[i][0], cx = c + mvt[i][1];
if (!is_ok(rx, cx))
continue;
vis[rx][cx] = true;
if (grid[rx][cx] == grid[r][c])
{
depth[rx][cx] = depth[r][c];
q.push_front({rx, cx});
}
else
{
depth[rx][cx] = depth[r][c] + 1;
q.push_back({rx, cx});
}
}
}
cout << res << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |