#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> xy = {1, 0, -1, 0}, yx = {0, 1, 0, -1};
const int mxN = 4e3+10;
int d[mxN][mxN], n, m;
char matrix[mxN][mxN];
bool valid(int a, int b) {
if(a < 0 || a >= n || b < 0 || b >= m ||
matrix[a][b] == '.') {
return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
int ans = 1;
deque<array<int, 2>> q;
q.push_back({0, 0});
d[0][0] = 1;
while(q.size()) {
auto node = q.front();
q.pop_front();
ans = max(ans, d[node[0]][node[1]]);
for(int i = 0; i < 4; i++) {
int a = node[0] + xy[i], b = node[1] + yx[i];
if(valid(a, b) && d[a][b] == 0) {
if(matrix[a][b] == matrix[node[0]][node[1]]) {
d[a][b] = d[node[0]][node[1]];
q.push_front({a, b});
}
else {
d[a][b] = d[node[0]][node[1]]+1;
q.push_back({a, b});
}
}
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7260 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
6 ms |
7156 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4776 KB |
Output is correct |
9 |
Correct |
1 ms |
5108 KB |
Output is correct |
10 |
Correct |
2 ms |
5572 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
5 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 |
7124 KB |
Output is correct |
16 |
Correct |
11 ms |
7260 KB |
Output is correct |
17 |
Correct |
10 ms |
7004 KB |
Output is correct |
18 |
Correct |
6 ms |
7040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
32092 KB |
Output is correct |
2 |
Correct |
40 ms |
16776 KB |
Output is correct |
3 |
Correct |
245 ms |
57996 KB |
Output is correct |
4 |
Correct |
69 ms |
31572 KB |
Output is correct |
5 |
Correct |
172 ms |
46892 KB |
Output is correct |
6 |
Correct |
507 ms |
92872 KB |
Output is correct |
7 |
Correct |
11 ms |
32856 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 |
9 ms |
32600 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
38 ms |
16772 KB |
Output is correct |
14 |
Correct |
24 ms |
11868 KB |
Output is correct |
15 |
Correct |
22 ms |
14216 KB |
Output is correct |
16 |
Correct |
19 ms |
7516 KB |
Output is correct |
17 |
Correct |
95 ms |
26956 KB |
Output is correct |
18 |
Correct |
81 ms |
33752 KB |
Output is correct |
19 |
Correct |
72 ms |
31568 KB |
Output is correct |
20 |
Correct |
59 ms |
25372 KB |
Output is correct |
21 |
Correct |
146 ms |
45396 KB |
Output is correct |
22 |
Correct |
170 ms |
46724 KB |
Output is correct |
23 |
Correct |
180 ms |
38264 KB |
Output is correct |
24 |
Correct |
144 ms |
42832 KB |
Output is correct |
25 |
Correct |
386 ms |
78984 KB |
Output is correct |
26 |
Correct |
305 ms |
131344 KB |
Output is correct |
27 |
Correct |
417 ms |
113528 KB |
Output is correct |
28 |
Correct |
511 ms |
92872 KB |
Output is correct |
29 |
Correct |
501 ms |
90540 KB |
Output is correct |
30 |
Correct |
467 ms |
96764 KB |
Output is correct |
31 |
Correct |
402 ms |
66800 KB |
Output is correct |
32 |
Correct |
389 ms |
100856 KB |
Output is correct |