#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
#include <deque>
#include <numeric>
using namespace std;
#define FOR(i,L,R) for (int i = L; i < R; i++)
#define FOR_STEP(i,L,R, STEP) for (int i = L; i < R; i+= STEP)
struct coord {
int x;
int y;
};
int main(){
int H, W;
cin >> H >> W;
char array[H][W];
int d[H][W];
// use '.' as visited
FOR(i, 0, H) {
FOR(j, 0, W) {
char x; cin >> x;
array[i][j] = x;
d[i][j] = INT_MAX;
}
}
int left[] = {-1, 1, 0, 0};
int right[] = {0, 0, -1, 1};
deque<coord> bfs_queue;
bfs_queue.push_front({0, 0});
d[0][0] = 0;
while (!bfs_queue.empty()) {
coord topCoord = bfs_queue.front();
bfs_queue.pop_front();
FOR (i, 0, 4) {
int nx = topCoord.x + left[i];
int ny = topCoord.y + right[i];
if (nx >=0 && nx < H && ny <W && ny >= 0) {
if (array[nx][ny] != '.') {
if (array[nx][ny] == array[topCoord.x][topCoord.y] && d[topCoord.x][topCoord.y] < d[nx][ny]) {
bfs_queue.push_front({nx, ny});
d[nx][ny] = d[topCoord.x][topCoord.y];
} else if (array[nx][ny] != array[topCoord.x][topCoord.y] && d[topCoord.x][topCoord.y] + 1 < d[nx][ny]){
bfs_queue.push_back({nx, ny});
d[nx][ny] = d[topCoord.x][topCoord.y] + 1;
}
}
}
}
array[topCoord.x][topCoord.y] = '.';
}
int maxDepth = 0;
FOR (i, 0, H) {
FOR (j, 0, W) {
if (d[i][j] != INT_MAX && d[i][j] > maxDepth) {
maxDepth = d[i][j];
}
}
}
cout << maxDepth + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1880 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 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 |
444 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
856 KB |
Output is correct |
12 |
Correct |
6 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
15 ms |
1884 KB |
Output is correct |
16 |
Correct |
18 ms |
1840 KB |
Output is correct |
17 |
Correct |
12 ms |
1628 KB |
Output is correct |
18 |
Correct |
9 ms |
1332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
73 ms |
9556 KB |
Output is correct |
3 |
Correct |
646 ms |
94280 KB |
Output is correct |
4 |
Correct |
152 ms |
22356 KB |
Output is correct |
5 |
Correct |
351 ms |
53072 KB |
Output is correct |
6 |
Correct |
1042 ms |
107340 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
4 ms |
760 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
69 ms |
9560 KB |
Output is correct |
14 |
Correct |
42 ms |
5716 KB |
Output is correct |
15 |
Correct |
38 ms |
6228 KB |
Output is correct |
16 |
Correct |
32 ms |
4184 KB |
Output is correct |
17 |
Correct |
195 ms |
24064 KB |
Output is correct |
18 |
Correct |
147 ms |
23892 KB |
Output is correct |
19 |
Correct |
152 ms |
22356 KB |
Output is correct |
20 |
Correct |
137 ms |
20432 KB |
Output is correct |
21 |
Correct |
367 ms |
54920 KB |
Output is correct |
22 |
Correct |
331 ms |
53136 KB |
Output is correct |
23 |
Correct |
365 ms |
45904 KB |
Output is correct |
24 |
Correct |
352 ms |
53588 KB |
Output is correct |
25 |
Correct |
646 ms |
94288 KB |
Output is correct |
26 |
Correct |
644 ms |
122288 KB |
Output is correct |
27 |
Correct |
880 ms |
111600 KB |
Output is correct |
28 |
Correct |
996 ms |
107480 KB |
Output is correct |
29 |
Correct |
1028 ms |
107740 KB |
Output is correct |
30 |
Correct |
950 ms |
106964 KB |
Output is correct |
31 |
Correct |
737 ms |
60756 KB |
Output is correct |
32 |
Correct |
804 ms |
109680 KB |
Output is correct |