#include <bits/stdc++.h>
#define mod 1000000007
#define endl '\n'
using namespace std;
int idk(char a,char b){
if(a=='N'){
if(b=='N')return 0;
if(b=='E')return 1;
if(b=='S')return 2;
if(b=='W')return 3;
}
if(a=='E'){
if(b=='N')return 3;
if(b=='E')return 0;
if(b=='S')return 1;
if(b=='W')return 2;
}
if(a=='S'){
if(b=='N')return 2;
if(b=='E')return 3;
if(b=='S')return 0;
if(b=='W')return 1;
}
if(a=='W'){
if(b=='N')return 1;
if(b=='E')return 2;
if(b=='S')return 3;
if(b=='W')return 0;
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
char arr[n][m];
for(int i=0;i<n;i++){
for(int w=0;w<m;w++){
cin>>arr[i][w];
}
}
priority_queue<pair<long long,pair<int,int>>,vector<pair<long long,pair<int,int>>>,greater<pair<long long,pair<int,int>>>>pq;
bool bl[n][m];
memset(bl,false,sizeof(bl));
pq.push({0,{0,0}});
while(pq.size()){
long long v=pq.top().first;
int i=pq.top().second.first;
int w=pq.top().second.second;
if(i==n-1&&w==m-1){cout<<v;return 0;}
pq.pop();
if(bl[i][w]||arr[i][w]=='X')continue;
bl[i][w]=true;
if(i>0&&!bl[i-1][w]/*&&(arr[i-1][w]!='X'||(i-1==n-1&&w==m-1))*/)pq.push({v+idk(arr[i][w],'N'),{i-1,w}});
if(i<n-1&&!bl[i+1][w]/*&&(arr[i+1][w]!='X'||(i+1==n-1&&w==m-1))*/)pq.push({v+idk(arr[i][w],'S'),{i+1,w}});
if(w>0&&!bl[i][w-1]/*&&(arr[i][w-1]!='X'||(i==n-1&&w-1==m-1))*/)pq.push({v+idk(arr[i][w],'W'),{i,w-1}});
if(w<m-1&&!bl[i][w+1]/*&&(arr[i][w+1]!='X'||(i==n-1&&w+1==m-1))*/)pq.push({v+idk(arr[i][w],'E'),{i,w+1}});
}
cout<<-1;
}
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
//0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
//
//1 3 2 4
Compilation message
adventure.cpp: In function 'int idk(char, char)':
adventure.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
30 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
268 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
268 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
7 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
8 ms |
376 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
83 ms |
884 KB |
Output is correct |
40 |
Correct |
87 ms |
856 KB |
Output is correct |
41 |
Correct |
4 ms |
684 KB |
Output is correct |
42 |
Correct |
85 ms |
848 KB |
Output is correct |
43 |
Correct |
54 ms |
4952 KB |
Output is correct |
44 |
Correct |
4 ms |
716 KB |
Output is correct |