#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
vector<pair<int,int>> dirs = {{0,-1},{0,1},{-1,0},{1,0}};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int R, C; cin >> R >> C;
vector<string> v(R);
for(auto &x: v) cin >> x;
vector<vector<bool>> visited(R,vector<bool>(C,false));
int cnts = 0;
queue<pair<int,int>> rabbits;
queue<pair<int,int>> foxes;
if(v[0][0] == 'R') {
rabbits.push({0,0});
} else if(v[0][0] == 'F') {
foxes.push({0,0});
}
while(true) {
// cout << "rabbits: " << rabbits.size() << " foxes: " << foxes.size() << "\n";
if(rabbits.size()) {
while(rabbits.size()) {
pair<int,int> p = rabbits.front();
rabbits.pop();
if(visited[p.first][p.second]) {
continue;
}
// cout << "rabbit bfs: " << p.first << " " << p.second << "\n";
visited[p.first][p.second] = true;
for(auto x : dirs) {
int r = p.first + x.first;
int c = p.second + x.second;
if(r >= 0 && r < R && c >= 0 && c < C) {
if(v[r][c] == 'F' && !visited[r][c]) {
foxes.push({r,c});
} else if(v[r][c] == 'R' && !visited[r][c]) {
rabbits.push({r,c});
}
}
}
}
} else if(foxes.size()) {
while(foxes.size()) {
pair<int,int> p = foxes.front();
foxes.pop();
if(visited[p.first][p.second]) {
continue;
}
// cout << "fox bfs: " << p.first << " " << p.second << "\n";
visited[p.first][p.second] = true;
for(auto x : dirs) {
int r = p.first + x.first;
int c = p.second + x.second;
if(r >= 0 && r < R && c >= 0 && c < C) {
if(v[r][c] == 'F' && !visited[r][c]) {
foxes.push({r,c});
} else if(v[r][c] == 'R' && !visited[r][c]) {
rabbits.push({r,c});
}
}
}
}
} else {
break;
}
cnts++;
}
cout << cnts << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
8 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
520 KB |
Output is correct |
12 |
Correct |
6 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
13 ms |
1088 KB |
Output is correct |
16 |
Correct |
16 ms |
1116 KB |
Output is correct |
17 |
Correct |
8 ms |
856 KB |
Output is correct |
18 |
Correct |
7 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
43 ms |
3932 KB |
Output is correct |
3 |
Correct |
195 ms |
32028 KB |
Output is correct |
4 |
Correct |
39 ms |
7252 KB |
Output is correct |
5 |
Correct |
103 ms |
18004 KB |
Output is correct |
6 |
Correct |
989 ms |
46580 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
46 ms |
3452 KB |
Output is correct |
14 |
Correct |
26 ms |
2140 KB |
Output is correct |
15 |
Correct |
10 ms |
2396 KB |
Output is correct |
16 |
Correct |
24 ms |
1884 KB |
Output is correct |
17 |
Correct |
109 ms |
7688 KB |
Output is correct |
18 |
Correct |
37 ms |
7624 KB |
Output is correct |
19 |
Correct |
38 ms |
7256 KB |
Output is correct |
20 |
Correct |
53 ms |
6992 KB |
Output is correct |
21 |
Correct |
111 ms |
16524 KB |
Output is correct |
22 |
Correct |
105 ms |
17960 KB |
Output is correct |
23 |
Correct |
221 ms |
14380 KB |
Output is correct |
24 |
Correct |
85 ms |
18236 KB |
Output is correct |
25 |
Correct |
232 ms |
31764 KB |
Output is correct |
26 |
Correct |
466 ms |
22028 KB |
Output is correct |
27 |
Correct |
645 ms |
31928 KB |
Output is correct |
28 |
Correct |
966 ms |
46156 KB |
Output is correct |
29 |
Correct |
908 ms |
42344 KB |
Output is correct |
30 |
Correct |
803 ms |
41256 KB |
Output is correct |
31 |
Correct |
808 ms |
19496 KB |
Output is correct |
32 |
Correct |
566 ms |
30980 KB |
Output is correct |