#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"
vector<int> e;
int find(int u){
return e[u] < 0 ? u : e[u] = find(e[u]);
}
void unite(int u, int v){
u = find(u), v = find(v);
if(u==v) return;
if(e[u] > e[v]) swap(u, v);
e[u] += e[v], e[v] = u;
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int h, w; cin >> h >> w;
int g[h][w];
e.assign(h*w, -1);
for(int i=0; i<h; ++i){
for(int j=0; j<w; ++j){
char c; cin >> c;
if(c=='.') g[i][j] = 0;
if(c=='F') g[i][j] = 1;
if(c=='R') g[i][j] = 2;
}
}
int dist[h][w];
for(int i=0; i<h; ++i)
fill(dist[i], dist[i]+w, 1e18);
dist[0][0] = 1;
deque<array<int, 2>> q;
q.push_back({0, 0});
int ans = 0;
while(!q.empty()){
int x = q.front()[0], y = q.front()[1], d = dist[x][y];
q.pop_front();
for(int k=0; k<4; ++k){
int i = x+dx[k], j = y+dy[k];
if(i<0 or j<0 or i==h or j==w or !g[i][j]) continue;
int w = g[i][j]!=g[x][y];
if(dist[i][j] > dist[x][y] + w){
dist[i][j] = dist[x][y] + w;
if(w) q.push_back({i, j});
else q.push_front({i, j});
}
}
ans = d;
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6124 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
11 ms |
4332 KB |
Output is correct |
5 |
Correct |
5 ms |
2284 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
4 ms |
1900 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
7 ms |
2412 KB |
Output is correct |
13 |
Correct |
5 ms |
2284 KB |
Output is correct |
14 |
Correct |
5 ms |
2284 KB |
Output is correct |
15 |
Correct |
17 ms |
6252 KB |
Output is correct |
16 |
Correct |
20 ms |
6124 KB |
Output is correct |
17 |
Correct |
17 ms |
5996 KB |
Output is correct |
18 |
Correct |
11 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1516 KB |
Output is correct |
2 |
Correct |
85 ms |
37100 KB |
Output is correct |
3 |
Correct |
611 ms |
376172 KB |
Output is correct |
4 |
Correct |
172 ms |
88556 KB |
Output is correct |
5 |
Correct |
374 ms |
211820 KB |
Output is correct |
6 |
Correct |
1150 ms |
402536 KB |
Output is correct |
7 |
Correct |
3 ms |
1388 KB |
Output is correct |
8 |
Correct |
3 ms |
1516 KB |
Output is correct |
9 |
Correct |
4 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1132 KB |
Output is correct |
11 |
Correct |
3 ms |
1388 KB |
Output is correct |
12 |
Correct |
2 ms |
876 KB |
Output is correct |
13 |
Correct |
85 ms |
37100 KB |
Output is correct |
14 |
Correct |
49 ms |
21484 KB |
Output is correct |
15 |
Correct |
49 ms |
23788 KB |
Output is correct |
16 |
Correct |
40 ms |
15340 KB |
Output is correct |
17 |
Correct |
231 ms |
95596 KB |
Output is correct |
18 |
Correct |
211 ms |
94316 KB |
Output is correct |
19 |
Correct |
173 ms |
88428 KB |
Output is correct |
20 |
Correct |
139 ms |
81132 KB |
Output is correct |
21 |
Correct |
361 ms |
218988 KB |
Output is correct |
22 |
Correct |
369 ms |
211820 KB |
Output is correct |
23 |
Correct |
436 ms |
182224 KB |
Output is correct |
24 |
Correct |
359 ms |
213868 KB |
Output is correct |
25 |
Correct |
996 ms |
376072 KB |
Output is correct |
26 |
Correct |
728 ms |
388332 KB |
Output is correct |
27 |
Correct |
953 ms |
411224 KB |
Output is correct |
28 |
Correct |
1151 ms |
402532 KB |
Output is correct |
29 |
Correct |
1134 ms |
403348 KB |
Output is correct |
30 |
Correct |
1063 ms |
397756 KB |
Output is correct |
31 |
Correct |
983 ms |
242028 KB |
Output is correct |
32 |
Correct |
877 ms |
407232 KB |
Output is correct |