#include<bits/stdc++.h>
#define pii pair<int,pair<int,int> >
#define fi first
#define s second
using namespace std;
int n,m,k,i,d[1005][1005],f,D,F[1005][1005];
char a[1005][1005];
priority_queue< pii , vector<pii>,greater<pii> >q;
int main(){
cin>>n>>m;
for(k=1;k<=n;k++){
for(i=1;i<=m;i++){
cin>>a[k][i]; d[k][i]=500*500*4;
}
}
d[1][1]=0;
q.push({0,{1,1}});
while(q.size()){
int x=q.top().s.fi;
int y=q.top().s.s;
int D=q.top().fi;
q.pop();
if(D>d[x][y]) continue;
F[x][y]=1;
if(a[x][y]=='X') continue;
if(y<m){
if(a[x][y]=='E') f=0;
else if(a[x][y]=='N') f=1;
else if(a[x][y]=='W') f=2;
else f=3;
if(d[x][y+1]>D+f){
d[x][y+1]=D+f;
q.push({d[x][y+1],{x,y+1}});
}
}
if(y>1){
if(a[x][y]=='W') f=0;
else if(a[x][y]=='S') f=1;
else if(a[x][y]=='E') f=2;
else f=3;
if(d[x][y-1]>D+f){
d[x][y-1]=D+f;
q.push({d[x][y-1],{x,y-1}});
}
}
if(x<n){
if(a[x][y]=='S') f=0;
else if(a[x][y]=='E') f=1;
else if(a[x][y]=='N') f=2;
else if(a[x][y]=='W') f=3;
if(d[x+1][y]>D+f){
d[x+1][y]=D+f;
q.push({d[x+1][y],{x+1,y}});
}
}//cout<<a[2][2]<<" "<<x<<" "<<y<<endl;
if(x>1){
if(a[x][y]=='N') f=0;
else if(a[x][y]=='W') f=1;
else if(a[x][y]=='S') f=2;
else if(a[x][y]=='E') f=3;
if(d[x-1][y]>D+f){
d[x-1][y]=D+f;
q.push({d[x-1][y],{x-1,y}});
}
}
}
if(!F[n][m])cout<<-1;
else cout<<d[n][m];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
8 ms |
896 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
8 ms |
1280 KB |
Output is correct |
38 |
Correct |
5 ms |
1664 KB |
Output is correct |
39 |
Correct |
88 ms |
4984 KB |
Output is correct |
40 |
Correct |
78 ms |
4984 KB |
Output is correct |
41 |
Correct |
23 ms |
3064 KB |
Output is correct |
42 |
Correct |
76 ms |
4984 KB |
Output is correct |
43 |
Correct |
113 ms |
8036 KB |
Output is correct |
44 |
Correct |
24 ms |
3064 KB |
Output is correct |