이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |