#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
bool vis[maxn][maxn];
char s[maxn][maxn];
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int n, m;
bool inside(int i, int j) {
return (0 <= i && i < n) && (0 <= j && j < m);
}
vector <pair <int, int>> can;
bool mark[maxn][maxn];
void dfs(int x, int y, char cur) {
vis[x][y] = true;
mark[x][y] = true;
char inv = (cur == 'T') ? 'B' : 'T';
for(int k = 0; k < 4; k++) {
int i = x + dx[k];
int j = y + dy[k];
if(inside(i, j) && s[i][j] == cur && !vis[i][j]) {
dfs(i, j, cur);
}
if(inside(i, j) && s[i][j] == inv && !mark[i][j]) {
mark[i][j] = true;
can.emplace_back(i, j);
}
}
}
int main() {
scanf("%d %d", &n, &m);
int total = 0;
for(int i = 0; i < n; i++) {
scanf("%s", s[i]);
for(int j = 0; j < m; j++) {
total += (s[i][j] != '*');
}
}
int ans = 0;
char cur = s[0][0];
can.emplace_back(0, 0);
while(can.size() > 0) {
auto tmp = can;
can.clear();
for(auto i : tmp) {
if(!vis[i.first][i.second]) {
dfs(i.first, i.second, cur);
}
}
cur = (cur == 'T') ? 'B' : 'T';
++ans;
}
printf("%d\n", ans);
return 0;
}
Compilation message
zoo.cpp: In function 'int main()':
zoo.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
zoo.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s[i]);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
9 ms |
4224 KB |
Output is correct |
17 |
Correct |
11 ms |
4352 KB |
Output is correct |
18 |
Correct |
10 ms |
4224 KB |
Output is correct |
19 |
Correct |
11 ms |
4096 KB |
Output is correct |
20 |
Correct |
11 ms |
4352 KB |
Output is correct |
21 |
Correct |
43 ms |
3584 KB |
Output is correct |
22 |
Correct |
44 ms |
4480 KB |
Output is correct |
23 |
Correct |
46 ms |
4480 KB |
Output is correct |
24 |
Correct |
46 ms |
4732 KB |
Output is correct |
25 |
Correct |
44 ms |
4608 KB |
Output is correct |
26 |
Correct |
43 ms |
4480 KB |
Output is correct |
27 |
Correct |
42 ms |
4480 KB |
Output is correct |
28 |
Correct |
42 ms |
4480 KB |
Output is correct |
29 |
Correct |
45 ms |
4608 KB |
Output is correct |
30 |
Correct |
45 ms |
4480 KB |
Output is correct |