#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector <string> s(h);
for ( auto &u: s ){
cin >> u;
for ( auto &v: u ){
v = (v == 'F' ? 0 : v == 'R' ? 1 : 2);
}
}
vector <vector<bool>> us(h, vector <bool> (w));
auto ok = [&](int u, int v){
return u >= 0 && u < h && v >= 0 && v < w && !us[u][v];
};
int cnt = 0, t = s[0][0];
vector <pair<int,int>> q;
q.pb({0, 0}); us[0][0] = 1;
while ( !q.empty() ){
vector <pair<int,int>> nxt;
for ( int i = 0; i < (int)q.size(); i++ ){
auto [u, v] = q[i];
for ( int j = 0; j < 4; j++ ){
int x = u + dx[j], y = v + dy[j];
if ( !ok(x, y) ) continue;
us[x][y] = 1;
if ( s[x][y] == t ){
q.pb({x, y});
} else if ( s[x][y] != 2 ) nxt.pb({x, y});
}
}
cnt += 1; t ^= 1;
swap(nxt, q);
}
cout << cnt;
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1268 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
2356 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
1116 KB |
Output is correct |
16 |
Correct |
8 ms |
1268 KB |
Output is correct |
17 |
Correct |
6 ms |
1076 KB |
Output is correct |
18 |
Correct |
5 ms |
2356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
828 KB |
Output is correct |
2 |
Correct |
32 ms |
4008 KB |
Output is correct |
3 |
Correct |
221 ms |
36132 KB |
Output is correct |
4 |
Correct |
41 ms |
8680 KB |
Output is correct |
5 |
Correct |
161 ms |
20304 KB |
Output is correct |
6 |
Correct |
599 ms |
134484 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
980 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 |
34 ms |
3856 KB |
Output is correct |
14 |
Correct |
20 ms |
2396 KB |
Output is correct |
15 |
Correct |
14 ms |
2648 KB |
Output is correct |
16 |
Correct |
16 ms |
2020 KB |
Output is correct |
17 |
Correct |
91 ms |
9248 KB |
Output is correct |
18 |
Correct |
56 ms |
9140 KB |
Output is correct |
19 |
Correct |
40 ms |
8528 KB |
Output is correct |
20 |
Correct |
49 ms |
8016 KB |
Output is correct |
21 |
Correct |
120 ms |
21076 KB |
Output is correct |
22 |
Correct |
163 ms |
20824 KB |
Output is correct |
23 |
Correct |
181 ms |
17744 KB |
Output is correct |
24 |
Correct |
132 ms |
20624 KB |
Output is correct |
25 |
Correct |
277 ms |
36188 KB |
Output is correct |
26 |
Correct |
337 ms |
159040 KB |
Output is correct |
27 |
Correct |
465 ms |
170556 KB |
Output is correct |
28 |
Correct |
554 ms |
134588 KB |
Output is correct |
29 |
Correct |
556 ms |
134552 KB |
Output is correct |
30 |
Correct |
498 ms |
199492 KB |
Output is correct |
31 |
Correct |
492 ms |
25296 KB |
Output is correct |
32 |
Correct |
419 ms |
168920 KB |
Output is correct |