#include <bits/stdc++.h>
#define int long long
using namespace std;
struct dirs_struct {
const int INF = 1e18;
int N = 0;
int E = 1;
int S = 2;
int W = 3;
int get_dir(char dir) {
if (dir == 'N') {
return N;
} else if (dir == 'E') {
return E;
} else if (dir == 'S') {
return S;
} else if (dir == 'W') {
return W;
} else {
return -1;
}
}
int get_dir(int dx, int dy) {
if (dx == 0 && dy == -1) {
return N;
} else if (dx == 1 && dy == 0) {
return E;
} else if (dx == 0 && dy == 1) {
return S;
} else if (dx == -1 && dy == 0) {
return W;
} else {
return -1;
}
}
int get_cost(char direction1, int dx, int dy) {
int dir1 = get_dir(direction1);
int dir2 = get_dir(dx, dy);
if (dir1 == -1 || dir2 == -1) {
return INF;
} else {
return ((dir2 - dir1) + 4) % 4;
}
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
const int INF = 1e18;
dirs_struct dirs;
int m, n;
cin >> m >> n;
vector<vector<char>> grid(m+2, vector<char>(n+2, 'X'));
for (int y=0; y<m; y++) {
for (int x=0; x<n; x++) {
cin >> grid[y+1][x+1];
}
}
vector<vector<int>> cost(m+2, vector<int>(n+2, INF));
priority_queue<tuple<int, int, int>,
vector<tuple<int, int, int>>,
greater<tuple<int, int, int>>> pq;
cost[1][1] = 0;
pq.emplace(0, 0, 0);
while (!pq.empty()) {
auto [c, x, y] = pq.top();
pq.pop();
//cerr << c << " " << x << " " << y << "\n";
if (grid[y+1][x+1] == 'X') { continue; }
array<pair<int, int>, 4> offsets {make_pair(1, 0), make_pair(-1, 0),
make_pair(0, 1), make_pair(0, -1)};
for (auto [dx, dy] : offsets) {
int new_cost = c + dirs.get_cost(grid[y+1][x+1], dx, dy);
if (cost[y+dy+1][x+dx+1] > new_cost) {
cost[y+dy+1][x+dx+1] = new_cost;
pq.emplace(new_cost, x+dx, y+dy);
}
}
}
if (cost[m][n] >= INF) {
cout << -1 << "\n";
} else {
cout << cost[m][n] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
312 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
6 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
7 ms |
588 KB |
Output is correct |
38 |
Correct |
1 ms |
704 KB |
Output is correct |
39 |
Correct |
69 ms |
2764 KB |
Output is correct |
40 |
Correct |
63 ms |
2852 KB |
Output is correct |
41 |
Correct |
6 ms |
2768 KB |
Output is correct |
42 |
Correct |
65 ms |
2852 KB |
Output is correct |
43 |
Correct |
135 ms |
9008 KB |
Output is correct |
44 |
Correct |
6 ms |
2764 KB |
Output is correct |