#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char>>grid;
vector<vector<bool>>vis;
bool found = 0;
bool check(int i, int j){
return (i >= 0 && i < n && j >= 0 && j < m);
}
void rec(int i, int j, char cur){
vis[i][j] = 1;
if(grid[i][j] == cur){
found = 1;
grid[i][j] = 'x';
}
if(check(i + 1, j) && !vis[i + 1][j] && (grid[i + 1][j] == 'x' || grid[i + 1][j] == cur))rec(i + 1, j, cur);
if(check(i, j + 1) && !vis[i][j + 1] && (grid[i][j + 1] == 'x' || grid[i][j + 1] == cur))rec(i, j + 1, cur);
if(check(i - 1, j) && !vis[i - 1][j] && (grid[i - 1][j] == 'x' || grid[i - 1][j] == cur))rec(i - 1, j, cur);
if(check(i, j - 1) && !vis[i][j - 1] && (grid[i][j - 1] == 'x' || grid[i][j - 1] == cur))rec(i, j - 1, cur);
}
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
cin >> n >> m;
grid.assign(n, vector<char>(m));
vis.assign(n, vector<bool>(m));
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
cin >> grid[i][j];
}
}
char c = grid[0][0];
int cnt = 0;
rec(0, 0, c);
while(found){
found = 0;
cnt++;
if(c == 'T')c = 'B';
else c = 'T';
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
vis[i][j] = 0;
}
}
rec(0, 0, c);
}
cout << cnt;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
552 KB |
Output is correct |
12 |
Correct |
7 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
552 KB |
Output is correct |
12 |
Correct |
7 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
58 ms |
5080 KB |
Output is correct |
17 |
Correct |
57 ms |
4876 KB |
Output is correct |
18 |
Correct |
53 ms |
3924 KB |
Output is correct |
19 |
Correct |
59 ms |
5540 KB |
Output is correct |
20 |
Correct |
50 ms |
4664 KB |
Output is correct |
21 |
Execution timed out |
2058 ms |
12392 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |