#include <bits/stdc++.h>
using namespace std;
int a[4005][4005];
int d[4005][4005];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int h, w; cin >> h >> w;
memset(d, -1, sizeof(d));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
char c; cin >> c;
if (c=='.') a[i][j]=1;
if (c=='R') a[i][j]=2;
if (c=='F') a[i][j] = 3;
}
}
d[1][1] = 1;
deque<pair<int,int>> q;
q.push_back({1, 1});
while (!q.empty()) {
int i = q.front().first, j=q.front().second;
q.pop_front();
vector<pair<pair<int,int>, int>> adj;
int x = a[i][j];
if (i==h && j==w) continue;
if (i+1 <= h && a[i+1][j]!=1) adj.push_back({{i+1, j}, (x==5-a[i+1][j])});
if (i-1 >= 1 && a[i-1][j]!=1) adj.push_back({{i-1, j}, (x==5-a[i-1][j])});
if (j+1 <= w && a[i][j+1]!=1) adj.push_back({{i, j+1}, (x==5-a[i][j+1])});
if (j-1 >= 1 && a[i][j-1] != 1) adj.push_back({{i, j-1}, (x==5-a[i][j-1])});
for (auto p : adj) {
int x= p.first.first, y=p.first.second, c = p.second;
if (d[x][y]==-1 || d[x][y] > d[i][j]+c) {
d[x][y] = d[i][j]+c;
if (c==0) {
q.push_front({x, y});
} else {
q.push_back({x, y});
}
}
}
}
int ans = 1;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ans = max(ans, d[i][j]);
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
72284 KB |
Output is correct |
2 |
Correct |
8 ms |
64092 KB |
Output is correct |
3 |
Correct |
8 ms |
64092 KB |
Output is correct |
4 |
Correct |
22 ms |
72540 KB |
Output is correct |
5 |
Correct |
11 ms |
68188 KB |
Output is correct |
6 |
Correct |
8 ms |
64200 KB |
Output is correct |
7 |
Correct |
8 ms |
64092 KB |
Output is correct |
8 |
Correct |
8 ms |
64092 KB |
Output is correct |
9 |
Correct |
9 ms |
66204 KB |
Output is correct |
10 |
Correct |
11 ms |
68188 KB |
Output is correct |
11 |
Correct |
11 ms |
68380 KB |
Output is correct |
12 |
Correct |
16 ms |
68352 KB |
Output is correct |
13 |
Correct |
11 ms |
68184 KB |
Output is correct |
14 |
Correct |
11 ms |
68184 KB |
Output is correct |
15 |
Correct |
27 ms |
72488 KB |
Output is correct |
16 |
Correct |
31 ms |
72284 KB |
Output is correct |
17 |
Correct |
22 ms |
72280 KB |
Output is correct |
18 |
Correct |
23 ms |
72796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
123740 KB |
Output is correct |
2 |
Correct |
83 ms |
84752 KB |
Output is correct |
3 |
Correct |
455 ms |
125936 KB |
Output is correct |
4 |
Correct |
115 ms |
92756 KB |
Output is correct |
5 |
Correct |
301 ms |
111188 KB |
Output is correct |
6 |
Correct |
1483 ms |
155128 KB |
Output is correct |
7 |
Correct |
16 ms |
125788 KB |
Output is correct |
8 |
Correct |
16 ms |
123740 KB |
Output is correct |
9 |
Correct |
11 ms |
64092 KB |
Output is correct |
10 |
Correct |
8 ms |
64404 KB |
Output is correct |
11 |
Correct |
15 ms |
125808 KB |
Output is correct |
12 |
Correct |
10 ms |
66140 KB |
Output is correct |
13 |
Correct |
81 ms |
84772 KB |
Output is correct |
14 |
Correct |
50 ms |
78672 KB |
Output is correct |
15 |
Correct |
39 ms |
80624 KB |
Output is correct |
16 |
Correct |
49 ms |
70236 KB |
Output is correct |
17 |
Correct |
192 ms |
95000 KB |
Output is correct |
18 |
Correct |
130 ms |
94996 KB |
Output is correct |
19 |
Correct |
110 ms |
92932 KB |
Output is correct |
20 |
Correct |
111 ms |
90868 KB |
Output is correct |
21 |
Correct |
273 ms |
113476 KB |
Output is correct |
22 |
Correct |
312 ms |
111644 KB |
Output is correct |
23 |
Correct |
366 ms |
103252 KB |
Output is correct |
24 |
Correct |
271 ms |
113760 KB |
Output is correct |
25 |
Correct |
671 ms |
126032 KB |
Output is correct |
26 |
Correct |
1014 ms |
181856 KB |
Output is correct |
27 |
Correct |
1327 ms |
168196 KB |
Output is correct |
28 |
Correct |
1482 ms |
155292 KB |
Output is correct |
29 |
Correct |
1470 ms |
153568 KB |
Output is correct |
30 |
Correct |
1403 ms |
159072 KB |
Output is correct |
31 |
Correct |
1008 ms |
124156 KB |
Output is correct |
32 |
Correct |
1145 ms |
157216 KB |
Output is correct |