#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define umin(x, y) x = min(x, y)
#define sq(x) ((x)*(x))
using namespace std;
const int N = 505;
const ll INF = 2e9 + 7;
int n, m;
char g[N][N];
int clc(char f, char t) {
if (f == t) return 0;
if (f == 'W') {
if (t == 'N') return 1;
if (t == 'E') return 2;
if (t == 'S') return 3;
}
if (f == 'S') {
if (t == 'W') return 1;
if (t == 'N') return 2;
if (t == 'E') return 3;
}
if (f == 'E') {
if (t == 'S') return 1;
if (t == 'W') return 2;
if (t == 'N') return 3;
}
if (t == 'E') return 1;
if (t == 'S') return 2;
if (t == 'W') return 3;
}
bool vis[N][N];
int dist[N][N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i <= n+1; i++) {
g[i][0] = 'X';
g[i][m+1] = 'X';
vis[i][0] = vis[i][m + 1] = 1;
}
for (int i = 0; i <= m + 1; i++) {
g[0][i] = 'X';
g[n+1][i] = 'X';
vis[0][i] = vis[n+1][i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> g[i][j];
dist[i][j] = INF;
}
}
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
dist[1][1] = 0;
pq.push({ 0, {1, 1} });
while (pq.size())
{
auto [cost, pnt] = pq.top();
auto [x, y] = pnt;
pq.pop();
if (g[x][y] == 'X') continue;
vis[x][y] = 1;
if (!vis[x-1][y] && dist[x - 1][y] > cost + clc(g[x][y], 'N')) {
dist[x - 1][y] = cost + clc(g[x][y], 'N');
pq.push({ dist[x - 1][y], {x-1, y} });
}
if (!vis[x+1][y] && dist[x + 1][y] > cost + clc(g[x][y], 'S')) {
dist[x + 1][y] = cost + clc(g[x][y], 'S');
pq.push({ dist[x + 1][y], {x+1, y} });
}
if (!vis[x][y+1] && dist[x][y + 1] > cost + clc(g[x][y], 'E')) {
dist[x][y + 1] = cost + clc(g[x][y], 'E');
pq.push({ dist[x][y + 1], {x, y+1} });
}
if (!vis[x][y-1] && dist[x][y - 1] > cost + clc(g[x][y], 'W')) {
dist[x][y - 1] = cost + clc(g[x][y], 'W');
pq.push({ dist[x][y - 1], {x, y-1} });
}
}
cout << (dist[n][m] == INF ? -1 : dist[n][m]);
}
Compilation message
adventure.cpp: In function 'int clc(char, char)':
adventure.cpp:34:1: warning: control reaches end of non-void function [-Wreturn-type]
34 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
5 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
1108 KB |
Output is correct |
39 |
Correct |
50 ms |
1832 KB |
Output is correct |
40 |
Correct |
56 ms |
1892 KB |
Output is correct |
41 |
Correct |
6 ms |
1748 KB |
Output is correct |
42 |
Correct |
56 ms |
1840 KB |
Output is correct |
43 |
Correct |
79 ms |
4960 KB |
Output is correct |
44 |
Correct |
4 ms |
1832 KB |
Output is correct |