#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAX = 1005;
pii dirs[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m;
int grid[MAX][MAX];
bool visited[MAX][MAX];
vector<pii> lastNodes, nodes;
void dfs(pii cord){
visited[cord.first][cord.second] = 1;
for(pii d:dirs){
pii cord1 = {cord.first + d.first, cord.second + d.second};
if(cord1.first >= 0 && cord1.first < n && cord1.second >= 0 && cord1.second < m && !visited[cord1.first][cord1.second]){
if(grid[cord1.first][cord1.second] == grid[cord.first][cord.second]){
dfs(cord1);
}
else if(grid[cord1.first][cord1.second] == 3 - grid[cord.first][cord.second]){
nodes.push_back(cord1);
}
}
}
}
int main(){
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
char c; cin >> c;
if(c == 'B'){
grid[i][j] = 1;
}
else if(c == 'T'){
grid[i][j] = 2;
}
}
}
int ans = 1;
int last = 0;
nodes.push_back({0, 0});
while(true){
lastNodes = nodes;
nodes.clear();
for(pii p:lastNodes){
if(!visited[p.first][p.second]){
dfs(p);
}
}
if(nodes.size() == 0){
break;
}
else{
++ans;
}
}
cout << ans << '\n';
}
Compilation message
zoo.cpp: In function 'int main()':
zoo.cpp:50:9: warning: unused variable 'last' [-Wunused-variable]
50 | int last = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
57 ms |
5732 KB |
Output is correct |
17 |
Correct |
57 ms |
5768 KB |
Output is correct |
18 |
Correct |
58 ms |
5764 KB |
Output is correct |
19 |
Correct |
60 ms |
5828 KB |
Output is correct |
20 |
Correct |
57 ms |
5804 KB |
Output is correct |
21 |
Correct |
89 ms |
5384 KB |
Output is correct |
22 |
Correct |
89 ms |
5236 KB |
Output is correct |
23 |
Correct |
90 ms |
5324 KB |
Output is correct |
24 |
Correct |
98 ms |
5428 KB |
Output is correct |
25 |
Correct |
91 ms |
5420 KB |
Output is correct |
26 |
Correct |
96 ms |
5404 KB |
Output is correct |
27 |
Correct |
88 ms |
5352 KB |
Output is correct |
28 |
Correct |
89 ms |
5204 KB |
Output is correct |
29 |
Correct |
96 ms |
5412 KB |
Output is correct |
30 |
Correct |
92 ms |
5320 KB |
Output is correct |