#include<bits/stdc++.h>
using namespace std;
#define lalala ios_base::sync_with_stdio(false);cin.tie(NULL);
//#define endl "\n"
#define ll long long
#define pb push_back
#define p push
#define f first
#define s second
ll int fark[5][5];
int main(){
lalala;
int n,m;
cin>>m>>n;
ll int uzak[n][m],arr[n][m];
for(int i=0;i<n;i++){
string str;cin>>str;
for(int j=0;j<str.size();j++){
uzak[i][j]=1000000000000000000;
if(str[j]=='E')arr[i][j]=1;
if(str[j]=='S')arr[i][j]=2;
if(str[j]=='W')arr[i][j]=3;
if(str[j]=='N')arr[i][j]=4;
if(str[j]=='X')arr[i][j]=0;
}
}
fark[0][1]=fark[0][2]=fark[0][3]=fark[0][4]=1000000000000000000;
fark[1][0]=fark[2][0]=fark[3][0]=fark[4][0]=1000000000000000000;
fark[1][2]=1;fark[1][3]=2;fark[1][4]=3;
fark[2][1]=3;fark[2][3]=1;fark[2][4]=2;
fark[3][1]=2;fark[3][2]=3;fark[3][4]=1;
fark[4][1]=1;;fark[4][2]=2;fark[4][3]=3;
priority_queue<pair<int,pair<int,int>>> pq;
pq.p({0,{0,0}});
while(pq.size()){
ll int deg=pq.top().f, x=pq.top().s.f, y=pq.top().s.s;
pq.pop();
//cout<<deg<<" "<<x<<" "<<y<<" "<<uzak[x][y]<<endl;
if(deg>=uzak[x][y])continue;
uzak[x][y]=deg;
int kim=arr[x][y];
if(kim==0)continue;
if(x!=0)pq.p({deg+fark[kim][4],{x-1,y}});
if(x!=n-1)pq.p({deg+fark[kim][2],{x+1,y}});
if(y!=0)pq.p({deg+fark[kim][3],{x,y-1}});
if(y!=m-1)pq.p({deg+fark[kim][1],{x,y+1}});
}
if(uzak[n-1][m-1]==1000000000000000000)uzak[n-1][m-1]=-1;
cout<<uzak[n-1][m-1]<<endl;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:18:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int j=0;j<str.size();j++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |