#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m, ans = 1;
cin >> n >> m;
vector <vector <char>> color(n+1, vector <char> (m+1));
vector <vector <int>> depth(n+1, vector <int> (m+1));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> color[i][j];
auto valid = [&](int x, int y, int n, int m, vector <vector <char>>& color) {return x >= 1 && y >= 1 && x <= n && y <= m && color[x][y] != '.';};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
deque <pair <int, int>> dq;
dq.push_front(make_pair(1, 1));
depth[1][1] = 1;
while (!dq.empty())
{
pair <int, int> cur = dq.front();
dq.pop_front();
ans = max(ans, depth[cur.first][cur.second]);
for (int i = 0; i < 4; ++i)
{
int x = cur.first + dx[i], y = cur.second + dy[i];
if (valid(x, y, n, m, color) && !depth[x][y])
{
if (color[cur.first][cur.second] == color[x][y])
{
depth[x][y] = depth[cur.first][cur.second];
dq.push_front(make_pair(x, y));
} else {
depth[x][y] = depth[cur.first][cur.second] + 1;
dq.push_back(make_pair(x, y));
}
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1900 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
9 ms |
1516 KB |
Output is correct |
5 |
Correct |
3 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
3 ms |
748 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
876 KB |
Output is correct |
13 |
Correct |
3 ms |
916 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
13 ms |
1900 KB |
Output is correct |
16 |
Correct |
14 ms |
1900 KB |
Output is correct |
17 |
Correct |
11 ms |
1772 KB |
Output is correct |
18 |
Correct |
11 ms |
1644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
876 KB |
Output is correct |
2 |
Correct |
61 ms |
9708 KB |
Output is correct |
3 |
Correct |
435 ms |
94572 KB |
Output is correct |
4 |
Correct |
115 ms |
22508 KB |
Output is correct |
5 |
Correct |
256 ms |
53484 KB |
Output is correct |
6 |
Correct |
863 ms |
108768 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
3 ms |
876 KB |
Output is correct |
9 |
Correct |
3 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
59 ms |
9708 KB |
Output is correct |
14 |
Correct |
34 ms |
5740 KB |
Output is correct |
15 |
Correct |
31 ms |
6252 KB |
Output is correct |
16 |
Correct |
29 ms |
4204 KB |
Output is correct |
17 |
Correct |
161 ms |
24300 KB |
Output is correct |
18 |
Correct |
131 ms |
24044 KB |
Output is correct |
19 |
Correct |
116 ms |
22636 KB |
Output is correct |
20 |
Correct |
103 ms |
20844 KB |
Output is correct |
21 |
Correct |
254 ms |
55276 KB |
Output is correct |
22 |
Correct |
255 ms |
53484 KB |
Output is correct |
23 |
Correct |
310 ms |
46316 KB |
Output is correct |
24 |
Correct |
251 ms |
53996 KB |
Output is correct |
25 |
Correct |
653 ms |
94640 KB |
Output is correct |
26 |
Correct |
559 ms |
122748 KB |
Output is correct |
27 |
Correct |
756 ms |
129256 KB |
Output is correct |
28 |
Correct |
894 ms |
108768 KB |
Output is correct |
29 |
Correct |
843 ms |
106188 KB |
Output is correct |
30 |
Correct |
807 ms |
111848 KB |
Output is correct |
31 |
Correct |
661 ms |
61420 KB |
Output is correct |
32 |
Correct |
688 ms |
118948 KB |
Output is correct |