#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 4e3+10;
char matrix[mxN][mxN];
vector<int> xy = {1, 0, -1, 0}, yx = {0, 1, 0, -1};
int n, m, d[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];
}
}
deque<array<int, 2>> q;
q.push_back({0, 0});
d[0][0] = 1;
int ans = 1;
while(!q.empty()) {
auto x = q.front()[0], y = q.front()[1];
q.pop_front();
ans = max(ans, d[x][y]);
for(int i = 0; i < 4; i++) {
int a = x + xy[i], b = y + yx[i];
if(valid(a, b) && d[a][b] == 0) {
if(matrix[a][b] == matrix[x][y]) {
d[a][b] = d[x][y];
q.push_front({a, b});
}
else {
d[a][b] = d[x][y]+1;
q.push_back({a, b});
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7516 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
7 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
5980 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
4 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5980 KB |
Output is correct |
14 |
Correct |
4 ms |
5980 KB |
Output is correct |
15 |
Correct |
9 ms |
7260 KB |
Output is correct |
16 |
Correct |
12 ms |
7512 KB |
Output is correct |
17 |
Correct |
8 ms |
7260 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
32344 KB |
Output is correct |
2 |
Correct |
42 ms |
14932 KB |
Output is correct |
3 |
Correct |
238 ms |
58192 KB |
Output is correct |
4 |
Correct |
70 ms |
31852 KB |
Output is correct |
5 |
Correct |
169 ms |
47304 KB |
Output is correct |
6 |
Correct |
498 ms |
92868 KB |
Output is correct |
7 |
Correct |
10 ms |
33112 KB |
Output is correct |
8 |
Correct |
9 ms |
32348 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
10 ms |
33012 KB |
Output is correct |
12 |
Correct |
1 ms |
3024 KB |
Output is correct |
13 |
Correct |
38 ms |
14932 KB |
Output is correct |
14 |
Correct |
22 ms |
12124 KB |
Output is correct |
15 |
Correct |
19 ms |
14428 KB |
Output is correct |
16 |
Correct |
18 ms |
7768 KB |
Output is correct |
17 |
Correct |
91 ms |
27184 KB |
Output is correct |
18 |
Correct |
73 ms |
34248 KB |
Output is correct |
19 |
Correct |
65 ms |
31828 KB |
Output is correct |
20 |
Correct |
57 ms |
23492 KB |
Output is correct |
21 |
Correct |
148 ms |
45504 KB |
Output is correct |
22 |
Correct |
171 ms |
47296 KB |
Output is correct |
23 |
Correct |
176 ms |
38484 KB |
Output is correct |
24 |
Correct |
142 ms |
43088 KB |
Output is correct |
25 |
Correct |
385 ms |
79000 KB |
Output is correct |
26 |
Correct |
294 ms |
121772 KB |
Output is correct |
27 |
Correct |
413 ms |
113736 KB |
Output is correct |
28 |
Correct |
485 ms |
92872 KB |
Output is correct |
29 |
Correct |
492 ms |
90624 KB |
Output is correct |
30 |
Correct |
445 ms |
97864 KB |
Output is correct |
31 |
Correct |
373 ms |
65360 KB |
Output is correct |
32 |
Correct |
374 ms |
100780 KB |
Output is correct |