#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pint;
const int D[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int h, w, grid[1000][1000];
bool seen[1000][1000];
queue<pint> q[3];
int main() {
cin >> h >> w;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
char c;
cin >> c;
if (c == 'T') grid[i][j] = 1;
else if (c == 'B') grid[i][j] = 2;
}
}
int n = 0;
q[grid[0][0]].emplace(0, -1);
for (int type = grid[0][0]; not q[type].empty(); ++n, type = 3 - type) {
while (not q[type].empty()) {
auto [x, y] = q[type].front();
q[type].pop();
for (int i = 0; i < 4; ++i) {
int x1 = x + D[i][0], y1 = y + D[i][1];
if (x1 >= 0 and x1 < h and y1 >= 0 and y1 < w) {
if (not seen[x1][y1]) {
seen[x1][y1] = true;
q[grid[x1][y1]].emplace(x1, y1);
}
}
}
}
}
cout << n;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
31 ms |
6352 KB |
Output is correct |
17 |
Correct |
38 ms |
6480 KB |
Output is correct |
18 |
Correct |
32 ms |
6484 KB |
Output is correct |
19 |
Correct |
36 ms |
6568 KB |
Output is correct |
20 |
Correct |
33 ms |
6484 KB |
Output is correct |
21 |
Correct |
55 ms |
8276 KB |
Output is correct |
22 |
Correct |
53 ms |
8272 KB |
Output is correct |
23 |
Correct |
56 ms |
8536 KB |
Output is correct |
24 |
Correct |
59 ms |
8752 KB |
Output is correct |
25 |
Correct |
57 ms |
8540 KB |
Output is correct |
26 |
Correct |
58 ms |
8532 KB |
Output is correct |
27 |
Correct |
50 ms |
8272 KB |
Output is correct |
28 |
Correct |
47 ms |
8272 KB |
Output is correct |
29 |
Correct |
58 ms |
8788 KB |
Output is correct |
30 |
Correct |
52 ms |
8536 KB |
Output is correct |