#include <bits/stdc++.h>
using namespace std;
int plansza[510][510][3];//czybylo koszt stan
int m,n;
char ch;
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
int main()
{
cin>>n>>m;
plansza[n][m][2]=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
plansza[i+1][j+1][1]=99999999;
cin>>ch;
if(ch=='N')
plansza[i+1][j+1][2]=1;
if(ch=='E')
plansza[i+1][j+1][2]=2;
if(ch=='S')
plansza[i+1][j+1][2]=3;
if(ch=='W')
plansza[i+1][j+1][2]=4;
}
}
if(plansza[1][1][2]==0){
cout<<-1;
return 0;
}
plansza[1][1][1]=0;
pq.push({0,{1,1}});
/*for(int i=-1;i<2;i+=2){
for(int j=0;j<2 && j>-2;j+=-i){
if(plansza[1+i+j][1+j][2]!=0){
pq.push({plansza[1][1][1]+(plansza[1][1][2]-1-(1+i+(j!=0))+4)%4,{1+i+j,1+j}});
plansza[1+i+j][1+j][1]=plansza[1][1][1]+(plansza[1][1][2]-1-(1+i+(j!=0))+4)%4;
//cout<<plansza[1][1][1]+(plansza[1][1][2]-1-(1+i+(j!=0))+4)%4<<" "<<1+i+j<<" "<<1+j<<" a ";
}
//pq.pop();}
}
}*/
while(!pq.empty()){
pair<int,pair<int,int>>ak=pq.top();
vector<pair<int,pair<int,int>>>v;
for(int i=-1;i<2;i+=2){
for(int j=0;j<2 && j>-2;j+=-i){
if(plansza[ak.second.first+i+j][ak.second.second+j][2]!=0 && plansza[ak.second.first+i+j][ak.second.second+j][1]>ak.first+(-(plansza[ak.second.first][ak.second.second][2]-1-(1+i+(j!=0)))+4)%4){
v.push_back({ak.first+(-(plansza[ak.second.first][ak.second.second][2]-1-(1+i+(j!=0)))+4)%4,{ak.second.first+i+j,ak.second.second+j}});
plansza[ak.second.first+i+j][ak.second.second+j][1]=ak.first+(-(plansza[ak.second.first][ak.second.second][2]-1-(1+i+(j!=0)))+4)%4;
//cout<<ak.first+(-(plansza[ak.second.first][ak.second.second][2]-1-(1+i+(j!=0)))+4)%4<<" "<<ak.second.first+i+j<<" "<<ak.second.second+j<<" ";
}
//pq.pop();}
}
}
//cout<<pq.top().first<<" "<<pq.top().second.first<<" "<<pq.top().second.second<<"\n";
pq.pop();
for(int i=0;i<v.size();i++)
pq.push(v[i]);
}
if(plansza[n][m][1]==99999999)
cout<<-1;
else
cout<<plansza[n][m][1];
return 0;
}
/*
4 4
SEEX
XESS
SNSS
ENEX*/
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
428 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
428 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
428 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
444 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
2648 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
7 ms |
2496 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
44 ms |
3696 KB |
Output is correct |
40 |
Correct |
44 ms |
3692 KB |
Output is correct |
41 |
Correct |
11 ms |
3672 KB |
Output is correct |
42 |
Correct |
45 ms |
3676 KB |
Output is correct |
43 |
Correct |
80 ms |
6672 KB |
Output is correct |
44 |
Correct |
11 ms |
3544 KB |
Output is correct |