#include <bits/stdc++.h>
using namespace std;
int H, W;
char grid[4001][4001];
vector<pair<int,int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void bfs(set<pair<int,int>> &from, set<pair<int,int>> &to, char cc){
while(!from.empty()){
auto temp = from.begin();
from.erase(from.begin());
//from.erase({temp->first, temp->second});
int x = temp->first, y = temp->second;
for(auto [dx, dy] : dir){
if(x+dx > -1 && x+dx < H && y+dy > -1 && y+dy < W && grid[x+dx][y+dy] != '.'){
if(grid[x+dx][y+dy] != cc) to.insert({x+dx, y+dy});
else from.insert({x+dx, y+dy});
grid[x+dx][y+dy] = '.';
}
}
}
}
int main(){
cin>>H>>W;
for(int i = 0; i < H; i++){
string s; cin>>s;
for(int j = 0; j < W; j++){
grid[i][j] = s[j];
}
}
int x = 0, y = 0, ans = 0;
char cc = grid[0][0];
set<pair<int,int>> F, R;
if(cc == 'F') F.insert({0,0});
else R.insert({0,0});
grid[0][0] = '.';
while(true){
if(cc == 'F' && F.empty()) break;
if(cc == 'R' && R.empty()) break;
if(cc == 'F')
bfs(F, R, cc);
else if(cc == 'R')
bfs(R, F, cc);
ans++;
if(cc == 'F') cc = 'R';
else if(cc == 'R') cc = 'F';
}
cout<<ans<<endl;
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:31:6: warning: unused variable 'x' [-Wunused-variable]
31 | int x = 0, y = 0, ans = 0;
| ^
tracks.cpp:31:13: warning: unused variable 'y' [-Wunused-variable]
31 | int x = 0, y = 0, ans = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2908 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
22 ms |
3248 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
4 ms |
2664 KB |
Output is correct |
11 |
Correct |
5 ms |
2652 KB |
Output is correct |
12 |
Correct |
10 ms |
2700 KB |
Output is correct |
13 |
Correct |
3 ms |
2672 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
22 ms |
2780 KB |
Output is correct |
16 |
Correct |
29 ms |
3008 KB |
Output is correct |
17 |
Correct |
14 ms |
2732 KB |
Output is correct |
18 |
Correct |
25 ms |
3288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15192 KB |
Output is correct |
2 |
Correct |
78 ms |
6748 KB |
Output is correct |
3 |
Correct |
412 ms |
15952 KB |
Output is correct |
4 |
Correct |
96 ms |
8788 KB |
Output is correct |
5 |
Correct |
244 ms |
12900 KB |
Output is correct |
6 |
Execution timed out |
2033 ms |
73560 KB |
Time limit exceeded |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15380 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
74 ms |
6764 KB |
Output is correct |
14 |
Correct |
54 ms |
4696 KB |
Output is correct |
15 |
Correct |
24 ms |
4696 KB |
Output is correct |
16 |
Correct |
39 ms |
2652 KB |
Output is correct |
17 |
Correct |
195 ms |
8784 KB |
Output is correct |
18 |
Correct |
96 ms |
8784 KB |
Output is correct |
19 |
Correct |
96 ms |
8788 KB |
Output is correct |
20 |
Correct |
89 ms |
8796 KB |
Output is correct |
21 |
Correct |
252 ms |
12884 KB |
Output is correct |
22 |
Correct |
236 ms |
12912 KB |
Output is correct |
23 |
Correct |
371 ms |
10832 KB |
Output is correct |
24 |
Correct |
209 ms |
12884 KB |
Output is correct |
25 |
Correct |
384 ms |
15968 KB |
Output is correct |
26 |
Correct |
1040 ms |
15116 KB |
Output is correct |
27 |
Correct |
1514 ms |
25376 KB |
Output is correct |
28 |
Execution timed out |
2076 ms |
73424 KB |
Time limit exceeded |
29 |
Execution timed out |
2074 ms |
64852 KB |
Time limit exceeded |
30 |
Correct |
1984 ms |
58772 KB |
Output is correct |
31 |
Correct |
1388 ms |
17508 KB |
Output is correct |
32 |
Correct |
1354 ms |
20560 KB |
Output is correct |