#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n, m;
int a[4009][4009];
int vis[4009][4009];
vector<pii> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
queue<pii> q[2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m;
for(int i = 0; i < n; ++i){
string s;
cin >> s;
for(int j = 0; j < m; ++j){
if(s[j] == 'F')
a[i][j] = 0;
else if(s[j] == 'R')
a[i][j] = 1;
else
a[i][j] = -1;
}
}
q[a[0][0]].push({0, 0});
int cur = a[0][0];
vis[0][0] = 1;
int ans = 0;
while(q[0].size() || q[1].size()){
++ans;
while(q[cur].size()){
pii x = q[cur].front();
q[cur].pop();
for(auto u : dir){
int x1 = x.ff + u.ff;
int y1 = x.ss + u.ss;
if(x1 < 0 || x1 >= n || y1 < 0 || y1 >= m)
continue;
if(a[x1][y1] != -1 && vis[x1][y1] == 0){
q[a[x1][y1]].push({x1, y1});
vis[x1][y1] = 1;
}
}
}
cur ^= 1;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
6528 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
11 ms |
5760 KB |
Output is correct |
5 |
Correct |
4 ms |
3328 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
1280 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
3 ms |
2336 KB |
Output is correct |
12 |
Correct |
7 ms |
3456 KB |
Output is correct |
13 |
Correct |
4 ms |
3328 KB |
Output is correct |
14 |
Correct |
4 ms |
3328 KB |
Output is correct |
15 |
Correct |
15 ms |
6400 KB |
Output is correct |
16 |
Correct |
17 ms |
6528 KB |
Output is correct |
17 |
Correct |
15 ms |
6272 KB |
Output is correct |
18 |
Correct |
10 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31488 KB |
Output is correct |
2 |
Correct |
69 ms |
21348 KB |
Output is correct |
3 |
Correct |
286 ms |
120700 KB |
Output is correct |
4 |
Correct |
109 ms |
47736 KB |
Output is correct |
5 |
Correct |
225 ms |
88824 KB |
Output is correct |
6 |
Correct |
1463 ms |
151688 KB |
Output is correct |
7 |
Correct |
21 ms |
32896 KB |
Output is correct |
8 |
Correct |
20 ms |
31480 KB |
Output is correct |
9 |
Correct |
2 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
20 ms |
32256 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
66 ms |
21368 KB |
Output is correct |
14 |
Correct |
36 ms |
14072 KB |
Output is correct |
15 |
Correct |
34 ms |
17152 KB |
Output is correct |
16 |
Correct |
26 ms |
8320 KB |
Output is correct |
17 |
Correct |
180 ms |
44916 KB |
Output is correct |
18 |
Correct |
126 ms |
51576 KB |
Output is correct |
19 |
Correct |
108 ms |
47864 KB |
Output is correct |
20 |
Correct |
111 ms |
39288 KB |
Output is correct |
21 |
Correct |
186 ms |
89336 KB |
Output is correct |
22 |
Correct |
220 ms |
88780 KB |
Output is correct |
23 |
Correct |
356 ms |
73208 KB |
Output is correct |
24 |
Correct |
195 ms |
85752 KB |
Output is correct |
25 |
Correct |
626 ms |
141560 KB |
Output is correct |
26 |
Correct |
813 ms |
122488 KB |
Output is correct |
27 |
Correct |
1132 ms |
143188 KB |
Output is correct |
28 |
Correct |
1460 ms |
151816 KB |
Output is correct |
29 |
Correct |
1380 ms |
150408 KB |
Output is correct |
30 |
Correct |
1263 ms |
146580 KB |
Output is correct |
31 |
Correct |
1043 ms |
111480 KB |
Output is correct |
32 |
Correct |
975 ms |
142324 KB |
Output is correct |