#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define chkmin(a, b) a = min(a, b)
#define x first
#define y second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int infinity = 1e9;
//int ans = infinity;
int dir_x[] = {-1, 0, 1, 0};
int dir_y[] = {0, 1, 0, -1};
int n, m;
//void Dfs(vvi &graph, int x, int y, vvb &visited, int count) {
// if (x < 0 || x >= n || y < 0 || y >= m) return;
// if (visited[x][y]) return;
// if (x == n - 1 && y == m - 1) {
// chkmin(ans, count);
// return;
// }
// if (graph[x][y] == 4) return;
// visited[x][y] = true;
// for (int i = 0; i < 4; i++) {
// Dfs(graph, x + dir_x[graph[x][y]], y + dir_y[graph[x][y]], visited, count + i);
// graph[x][y]++;
// graph[x][y] %= 4;
// }
// visited[x][y] = false;
//}
inline int Val(int i, int j) {
return i + n * j;
}
vector<vii> graph;
int Dijkstra() {
vi distances(n * m, infinity);
vb visited(n * m);
distances[0] = 0;
priority_queue<pii, vii, greater<pii>> pq;
pq.push({0, 0});
while (!pq.empty()) {
int node = pq.top().y, d = pq.top().x;
pq.pop();
if (visited[node]) continue;
visited[node] = true;
for (pii neighbor : graph[node]) {
if (distances[neighbor.x] > neighbor.y + d) {
distances[neighbor.x] = neighbor.y + d;
pq.push({neighbor.y + d, neighbor.x});
}
}
}
return distances[n * m - 1];
}
int main() {
cin >> n >> m;
graph.resize(n * m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
int curr = 0;
if (c == 'X') {
curr = 4;
} else if (c == 'S') {
curr = 2;
} else if (c == 'W') {
curr = 3;
} else if (c == 'E') {
curr = 1;
}
int node = Val(i, j);
if (curr != 4) {
for (int k = 0; k < 4; k++) {
i += dir_x[k], j += dir_y[k];
if (0 <= i && i < n && 0 <= j && j < m) {
graph[node].push_back({Val(i, j), (k - curr + 4) % 4});
}
i -= dir_x[k], j -= dir_y[k];
}
}
}
}
// vvb visited(n, vb(m));
// Dfs(graph, 0, 0, visited, 0);
int ans = Dijkstra();
cout << (ans == infinity ? -1 : ans);
return 0;
}
# |
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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 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 |
204 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 |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 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 |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 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 |
204 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 |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 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 |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
296 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
9 ms |
1796 KB |
Output is correct |
36 |
Correct |
1 ms |
420 KB |
Output is correct |
37 |
Correct |
11 ms |
1972 KB |
Output is correct |
38 |
Correct |
10 ms |
2908 KB |
Output is correct |
39 |
Correct |
106 ms |
16804 KB |
Output is correct |
40 |
Correct |
107 ms |
16836 KB |
Output is correct |
41 |
Correct |
58 ms |
16684 KB |
Output is correct |
42 |
Correct |
108 ms |
16780 KB |
Output is correct |
43 |
Correct |
151 ms |
21192 KB |
Output is correct |
44 |
Correct |
66 ms |
16708 KB |
Output is correct |