#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];
int dfs(pii cord){
visited[cord.first][cord.second] = 1;
int ans = 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] && grid[cord1.first][cord1.second] == grid[cord.first][cord.second]){
ans += dfs(cord1);
}
}
grid[cord.first][cord.second] = 3 - grid[cord.first][cord.second];
return ans;
}
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 = 0;
int last = 0;
while(true){
int a = dfs({0, 0});
if(last == a){
break;
}
else{
++ans;
}
last = a;
memset(visited, 0, sizeof(visited));
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1748 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
3 ms |
1748 KB |
Output is correct |
8 |
Correct |
8 ms |
1748 KB |
Output is correct |
9 |
Correct |
8 ms |
1748 KB |
Output is correct |
10 |
Correct |
9 ms |
1728 KB |
Output is correct |
11 |
Correct |
10 ms |
1772 KB |
Output is correct |
12 |
Correct |
13 ms |
1824 KB |
Output is correct |
13 |
Correct |
8 ms |
1620 KB |
Output is correct |
14 |
Correct |
7 ms |
1748 KB |
Output is correct |
15 |
Correct |
8 ms |
1736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1748 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
3 ms |
1748 KB |
Output is correct |
8 |
Correct |
8 ms |
1748 KB |
Output is correct |
9 |
Correct |
8 ms |
1748 KB |
Output is correct |
10 |
Correct |
9 ms |
1728 KB |
Output is correct |
11 |
Correct |
10 ms |
1772 KB |
Output is correct |
12 |
Correct |
13 ms |
1824 KB |
Output is correct |
13 |
Correct |
8 ms |
1620 KB |
Output is correct |
14 |
Correct |
7 ms |
1748 KB |
Output is correct |
15 |
Correct |
8 ms |
1736 KB |
Output is correct |
16 |
Correct |
71 ms |
7332 KB |
Output is correct |
17 |
Correct |
72 ms |
7552 KB |
Output is correct |
18 |
Correct |
74 ms |
6980 KB |
Output is correct |
19 |
Correct |
85 ms |
8292 KB |
Output is correct |
20 |
Correct |
71 ms |
6680 KB |
Output is correct |
21 |
Execution timed out |
2076 ms |
15240 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |