#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MAX 5e4 + 1
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 0x3F3F3F3F
#define INFLL 0x3F3F3F3F3F3F3F3FLL
#define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define sec second
#define fi first
#define elif else if
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int d[501][501], g[501][501], n, m;
int v[4] = {0, 1, 0, -1};
int f[4] = {-1, 0, 1, 0};
int wy[4] = {1, 2, 3, 4};
int dif(int a, int b)
{
if(a > b)return b + 4 - a;
return b - a;
}
void dijkstra(int x, int y)
{
d[x][y] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii> >, greater<pair<int, pii> > > q;
q.push({0, {x, y}});
while(!q.empty())
{
int a, b, w;
a = q.top().sec.fi;
b = q.top().sec.sec;
w = q.top().fi;
q.pop();
if(g[a][b] == 0)continue;
for(int i = 0; i<4; i++)
{
int xx = a + f[i], yy = b + v[i], way = wy[i];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m)
{
if(d[a][b] + dif(g[a][b], way) < d[xx][yy])
{
d[xx][yy] = d[a][b] + dif(g[a][b], way);
q.push({d[xx][yy], {xx, yy}});
}
}
}
}
}
void $()
{
cin >> n >> m;
for(int i = 0; i<501; i++)
{
for(int j = 0; j<501; j++)
{
d[i][j] = INF;
}
}
for(int i = 1 ; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
char c;
int k = 0;
cin >> c;
if(c == 'N')k = 1;
if(c == 'E')k = 2;
if(c == 'S')k = 3;
if(c == 'W')k = 4;
g[i][j] = k;
}
}
dijkstra(1, 1);
if(d[n][m] == INF)d[n][m] = -1;
cout << d[n][m];
}
int main()
{
OPT
int t = 1;
while(t--)
$();
}
Compilation message
adventure.cpp: In function 'void dijkstra(int, int)':
adventure.cpp:41:19: warning: variable 'w' set but not used [-Wunused-but-set-variable]
41 | int a, b, w;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1220 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1220 KB |
Output is correct |
4 |
Correct |
1 ms |
1192 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1224 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1220 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1220 KB |
Output is correct |
28 |
Correct |
1 ms |
1192 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
1 ms |
1224 KB |
Output is correct |
34 |
Correct |
1 ms |
1236 KB |
Output is correct |
35 |
Correct |
5 ms |
1396 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 KB |
Output is correct |
37 |
Correct |
7 ms |
1488 KB |
Output is correct |
38 |
Correct |
2 ms |
1752 KB |
Output is correct |
39 |
Correct |
49 ms |
2516 KB |
Output is correct |
40 |
Correct |
58 ms |
2556 KB |
Output is correct |
41 |
Correct |
7 ms |
2476 KB |
Output is correct |
42 |
Correct |
52 ms |
2544 KB |
Output is correct |
43 |
Correct |
89 ms |
5696 KB |
Output is correct |
44 |
Correct |
7 ms |
2516 KB |
Output is correct |