#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>>n>>m;
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();
uzak[x][y]=deg;
int kim=arr[x][y];
if(kim==0)continue;
if(x!=0&&uzak[x-1][y]>deg+fark[kim][4])pq.p({deg+fark[kim][4],{x-1,y}});
if(x!=n-1&&uzak[x+1][y]>deg+fark[kim][2])pq.p({deg+fark[kim][2],{x+1,y}});
if(y!=0&&uzak[x][y-1]>deg+fark[kim][3])pq.p({deg+fark[kim][3],{x,y-1}});
if(y!=m-1&&uzak[x][y+1]>deg+fark[kim][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++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
236 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
4 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
17 ms |
340 KB |
Output is correct |
6 |
Correct |
12 ms |
336 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
236 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
4 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
29 |
Correct |
17 ms |
340 KB |
Output is correct |
30 |
Correct |
12 ms |
336 KB |
Output is correct |
31 |
Correct |
3 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Execution timed out |
2066 ms |
852 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |