#include <bits/stdc++.h>
using namespace std;
#define int long long
#define OYY 1000000000005
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mid (start+end)/2
#define mod 1000000007
int n,m,dizi[505][505],uzak[505][505];
int cev=0,a,b;
char sor[505][505];
void dijk(){
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
uzak[i][j]=-1;
}
}
priority_queue <pair<int,pair<int,int>>> q;//
q.push({0,{0,0}});
while(!q.empty()){
int a=q.top().second.first;
int b=q.top().second.second;
int cost=q.top().first;
cost*=-1;
q.pop();
//uzak[b][a]=cost;
if((a<0 || a>=m || b<0 || b>=n)){//SINIR dışında
continue;
}
if(uzak[b][a]!=-1) continue;
uzak[b][a]=cost;
if(dizi[b][a]==-1)continue;
for(int i=0;i<4;i++){
int gx=(dizi[b][a]+i)%4;
int gy=(dizi[b][a]+i)%4;
if(gx==0)gx=0;
else if(gx==1)gx=1;
else if(gx==2)gx=0;
else if(gx==3)gx=-1;
//////////////////////////////////////////////////////////////
if(gy==0)gy=-1;
else if(gy==1)gy=0;
else if(gy==2)gy=1;
else if(gy==3)gy=0;
q.push({-(cost+i),{a+gx,b+gy}});
}
}
}
int32_t main(){
// faster
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>sor[i][j];
char h=sor[i][j];
if(h=='N')dizi[i][j]=0;
else if(h=='E')dizi[i][j]=1;
else if(h=='S')dizi[i][j]=2;
else if(h=='W')dizi[i][j]=3;
else if(h=='X')dizi[i][j]=-1;
uzak[i][j]=-1;
}
}
if(n==1){
cev=0;
bool stop=true;
for(int i=0;i<n && stop;i++){
for(int j=0;j<m && stop;j++){
if(sor[i][j]=='X'){
if(!(i==n-1 && j==m-1)){
cev=-1;
stop=false;
}
}
else if(sor[i][j]=='N'){
cev++;
}
else if(sor[i][j]=='S'){
cev+=3;
}
else if(sor[i][j]=='W'){
cev+=2;
}
else continue;
}
}
cout<<cev<<'\n';
return 0;
}
dijk();
/*for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<uzak[i][j]<<" ";
}
cout<<endl;
}*/
cout<<uzak[n-1][m-1]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
304 KB |
Output is correct |
35 |
Correct |
12 ms |
840 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
14 ms |
1280 KB |
Output is correct |
38 |
Correct |
3 ms |
2516 KB |
Output is correct |
39 |
Correct |
130 ms |
4916 KB |
Output is correct |
40 |
Correct |
142 ms |
5040 KB |
Output is correct |
41 |
Correct |
15 ms |
4764 KB |
Output is correct |
42 |
Correct |
124 ms |
5032 KB |
Output is correct |
43 |
Correct |
246 ms |
29508 KB |
Output is correct |
44 |
Correct |
15 ms |
4652 KB |
Output is correct |