#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define gecmio_ulan cin.tie(0);ios_base::sync_with_stdio(false);
int n, m;
int explored = 0;
int mat[4005][4005];
queue<pair<int, int> > a;
void dfs(int y, int x, int type) {
if (x >= m || y >= n || x < 0 || y < 0) return;
if (mat[y][x] == 2) return;
if (mat[y][x] != type) {
a.push({y, x});
return;
}
explored++;
mat[y][x] = 2;
dfs(y + 1, x, type);
dfs(y - 1, x, type);
dfs(y, x + 1, type);
dfs(y, x - 1, type);
}
int main() {
gecmio_ulan
cin >> n >> m;
char c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
if (c == 'R') {
mat[i][j] = 0;
}
else if (c == 'F') {
mat[i][j] = 1;
}
else {
mat[i][j] = 2;
explored++;
}
}
}
int ans = 0;
bool cur;
if (mat[0][0] == 0)cur = 0;
else cur = 1;
a.push({0, 0});
while (explored != m * n) {
while (a.size()) {
pair<int, int> tmp = a.front();
a.pop();
if (mat[tmp.first][tmp.second] == 2) continue;
else if (mat[tmp.first][tmp.second] != cur) {
a.push({tmp.first, tmp.second});
break;
}
dfs(tmp.first, tmp.second, cur);
}
ans++;
cur = !cur;
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3660 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
9 ms |
3968 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
576 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
5 ms |
1616 KB |
Output is correct |
11 |
Correct |
3 ms |
1612 KB |
Output is correct |
12 |
Correct |
7 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
1868 KB |
Output is correct |
14 |
Correct |
5 ms |
1864 KB |
Output is correct |
15 |
Correct |
14 ms |
3592 KB |
Output is correct |
16 |
Correct |
16 ms |
3660 KB |
Output is correct |
17 |
Correct |
11 ms |
3404 KB |
Output is correct |
18 |
Correct |
8 ms |
4024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16076 KB |
Output is correct |
2 |
Correct |
57 ms |
13124 KB |
Output is correct |
3 |
Correct |
369 ms |
79072 KB |
Output is correct |
4 |
Correct |
97 ms |
26436 KB |
Output is correct |
5 |
Correct |
331 ms |
56464 KB |
Output is correct |
6 |
Correct |
776 ms |
130740 KB |
Output is correct |
7 |
Correct |
8 ms |
16856 KB |
Output is correct |
8 |
Correct |
8 ms |
16064 KB |
Output is correct |
9 |
Correct |
3 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
8 ms |
16588 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
56 ms |
13148 KB |
Output is correct |
14 |
Correct |
31 ms |
8516 KB |
Output is correct |
15 |
Correct |
29 ms |
9208 KB |
Output is correct |
16 |
Correct |
25 ms |
5184 KB |
Output is correct |
17 |
Correct |
138 ms |
28460 KB |
Output is correct |
18 |
Correct |
134 ms |
28104 KB |
Output is correct |
19 |
Correct |
87 ms |
26408 KB |
Output is correct |
20 |
Correct |
84 ms |
24356 KB |
Output is correct |
21 |
Correct |
245 ms |
58288 KB |
Output is correct |
22 |
Correct |
385 ms |
56480 KB |
Output is correct |
23 |
Correct |
281 ms |
47528 KB |
Output is correct |
24 |
Correct |
256 ms |
57680 KB |
Output is correct |
25 |
Correct |
552 ms |
79060 KB |
Output is correct |
26 |
Correct |
1054 ms |
834248 KB |
Output is correct |
27 |
Correct |
774 ms |
281680 KB |
Output is correct |
28 |
Correct |
804 ms |
130760 KB |
Output is correct |
29 |
Correct |
778 ms |
124336 KB |
Output is correct |
30 |
Correct |
758 ms |
167748 KB |
Output is correct |
31 |
Correct |
702 ms |
61892 KB |
Output is correct |
32 |
Correct |
805 ms |
299520 KB |
Output is correct |