#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;
}
}
plansza[1][1][1]=0;
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]+min((plansza[1][1][2]-1-(1+i+(j!=0))+4)%4,(1-plansza[1][1][2]+(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]);
}
cout<<plansza[n][m][1];
return 0;
}
/*
4 4
SEEX
XESS
SNSS
ENEX*/
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:54: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]
54 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |