#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define all(x) x.begin(), x.end()
#define fastio\
ios_base::sync_with_stdio(0);\
cin.tie(0);\
cout.tie(0)\
const int sz = 505;
const int inf = 1e9 + 7;
int w[4][4] = {{0, 2, 1, 3}, {2, 0, 3, 1}, {3, 1, 0, 2}, {1, 3, 2, 0}};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
ll d[sz][sz], n, m;
char adj[sz][sz];
void djikstra(){
map<int, char> ind;
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
pq.push({0, {1, 1}});
d[1][1] = 0;
ind['N'] = 0, ind['S'] = 1, ind['E'] = 2, ind['W'] = 3;
while(!pq.empty()){
int x = pq.top().second.first, y = pq.top().second.second;
pq.pop();
for(int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
char c = adj[x][y];
if(c == 'X') break;
if(d[xx][yy] > d[x][y] + w[ind[c]][i]){
d[xx][yy] = d[x][y] + w[ind[c]][i];
pq.push({d[xx][yy], {xx, yy}});
}
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> adj[i][j];
d[i][j] = inf;
}
}
djikstra();
if(d[n][m] == inf) cout << -1;
else cout << d[n][m];
}
Compilation message
adventure.cpp: In function 'void djikstra()':
adventure.cpp:42:46: warning: array subscript has type 'char' [-Wchar-subscripts]
42 | if(d[xx][yy] > d[x][y] + w[ind[c]][i]){
| ^
adventure.cpp:43:47: warning: array subscript has type 'char' [-Wchar-subscripts]
43 | d[xx][yy] = d[x][y] + w[ind[c]][i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2432 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2432 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2548 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2500 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2604 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2624 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2432 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2548 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2500 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2604 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
1 ms |
2384 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2624 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
5 ms |
2556 KB |
Output is correct |
36 |
Correct |
1 ms |
2384 KB |
Output is correct |
37 |
Correct |
6 ms |
2384 KB |
Output is correct |
38 |
Correct |
2 ms |
2640 KB |
Output is correct |
39 |
Correct |
51 ms |
2920 KB |
Output is correct |
40 |
Correct |
48 ms |
2896 KB |
Output is correct |
41 |
Correct |
8 ms |
3064 KB |
Output is correct |
42 |
Correct |
46 ms |
2940 KB |
Output is correct |
43 |
Correct |
68 ms |
6596 KB |
Output is correct |
44 |
Correct |
13 ms |
2896 KB |
Output is correct |