#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 |
1 |
Correct |
11 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
6 ms |
7004 KB |
Output is correct |
5 |
Correct |
3 ms |
5720 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
3 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
4 ms |
5976 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5724 KB |
Output is correct |
15 |
Correct |
9 ms |
7048 KB |
Output is correct |
16 |
Correct |
10 ms |
7256 KB |
Output is correct |
17 |
Correct |
8 ms |
7004 KB |
Output is correct |
18 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
32092 KB |
Output is correct |
2 |
Correct |
41 ms |
16516 KB |
Output is correct |
3 |
Correct |
249 ms |
62292 KB |
Output is correct |
4 |
Correct |
72 ms |
31660 KB |
Output is correct |
5 |
Correct |
172 ms |
48260 KB |
Output is correct |
6 |
Correct |
553 ms |
97204 KB |
Output is correct |
7 |
Correct |
10 ms |
32860 KB |
Output is correct |
8 |
Correct |
10 ms |
32092 KB |
Output is correct |
9 |
Correct |
2 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
10 ms |
32600 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
40 ms |
16560 KB |
Output is correct |
14 |
Correct |
23 ms |
11856 KB |
Output is correct |
15 |
Correct |
22 ms |
14168 KB |
Output is correct |
16 |
Correct |
21 ms |
7360 KB |
Output is correct |
17 |
Correct |
103 ms |
26848 KB |
Output is correct |
18 |
Correct |
83 ms |
33616 KB |
Output is correct |
19 |
Correct |
70 ms |
31568 KB |
Output is correct |
20 |
Correct |
71 ms |
25172 KB |
Output is correct |
21 |
Correct |
178 ms |
46976 KB |
Output is correct |
22 |
Correct |
155 ms |
48464 KB |
Output is correct |
23 |
Correct |
194 ms |
39020 KB |
Output is correct |
24 |
Correct |
156 ms |
44652 KB |
Output is correct |
25 |
Correct |
430 ms |
82792 KB |
Output is correct |
26 |
Correct |
340 ms |
125948 KB |
Output is correct |
27 |
Correct |
469 ms |
107948 KB |
Output is correct |
28 |
Correct |
546 ms |
93144 KB |
Output is correct |
29 |
Correct |
556 ms |
89316 KB |
Output is correct |
30 |
Correct |
505 ms |
97432 KB |
Output is correct |
31 |
Correct |
413 ms |
66508 KB |
Output is correct |
32 |
Correct |
447 ms |
98144 KB |
Output is correct |