#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 = 509;
const ll INF = 2e9 + 7;
int n, m;
char g[N][N], gg[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[0][i] = 'X';
g[i][m+1] = 'X';
}
for (int i = 0; i <= m + 1; i++) {
g[i][0] = 'X';
g[n+1][i] = 'X';
}
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 (vis[x][y] || g[x][y] == 'X') continue;
vis[x][y] = 1;
if (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 (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 (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 (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 |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
26 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |