#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
1624 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
856 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
936 KB |
Output is correct |
15 |
Correct |
10 ms |
1884 KB |
Output is correct |
16 |
Correct |
11 ms |
1884 KB |
Output is correct |
17 |
Correct |
8 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1368 KB |
Output is correct |
2 |
Correct |
49 ms |
9968 KB |
Output is correct |
3 |
Correct |
293 ms |
96932 KB |
Output is correct |
4 |
Correct |
79 ms |
23072 KB |
Output is correct |
5 |
Correct |
197 ms |
54640 KB |
Output is correct |
6 |
Correct |
762 ms |
111240 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
43 ms |
9820 KB |
Output is correct |
14 |
Correct |
27 ms |
5980 KB |
Output is correct |
15 |
Correct |
19 ms |
6492 KB |
Output is correct |
16 |
Correct |
21 ms |
4348 KB |
Output is correct |
17 |
Correct |
133 ms |
25172 KB |
Output is correct |
18 |
Correct |
82 ms |
24656 KB |
Output is correct |
19 |
Correct |
76 ms |
23168 KB |
Output is correct |
20 |
Correct |
67 ms |
21084 KB |
Output is correct |
21 |
Correct |
179 ms |
56636 KB |
Output is correct |
22 |
Correct |
198 ms |
54640 KB |
Output is correct |
23 |
Correct |
224 ms |
47144 KB |
Output is correct |
24 |
Correct |
177 ms |
55304 KB |
Output is correct |
25 |
Correct |
526 ms |
96712 KB |
Output is correct |
26 |
Correct |
774 ms |
124496 KB |
Output is correct |
27 |
Correct |
754 ms |
106644 KB |
Output is correct |
28 |
Correct |
781 ms |
95528 KB |
Output is correct |
29 |
Correct |
768 ms |
92904 KB |
Output is correct |
30 |
Correct |
772 ms |
98296 KB |
Output is correct |
31 |
Correct |
480 ms |
52660 KB |
Output is correct |
32 |
Correct |
767 ms |
105224 KB |
Output is correct |