#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
struct idk{
int i,j,w;
};
char mat[501][501];
int d[501][501];
vector<idk>graf[501][501];
bool operator <(const idk &a,const idk &b){
return a.w<b.w;
}
void dijkstra(int si,int sj){
set<pair<int,pair<int,int>>>setic;
setic.insert({si,{sj,0}});
d[si][sj]=0;
while(!setic.empty()){
int i = (*setic.begin()).se.fi;
int j = (*setic.begin()).se.se;
setic.erase(setic.begin());
for(idk v : graf[i][j]){
if(d[v.i][v.j]>d[i][j]+v.w){
setic.erase({d[v.i][v.j],{v.i,v.j}});
d[v.i][v.j]=d[i][j]+v.w;
setic.insert({d[v.i][v.j],{v.i,v.j}});
}
}
}
}
int calc(char c){
switch(c){
case 'N':return 0;
case 'W':return 1;
case 'S':return 2;
case 'E':return 3;
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
for(int i=0;i<501;i++)for(int j=0;j<501;j++)d[i][j]=INT_MAX;
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)cin>>mat[i][j];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='X')continue;
if(i!=0)graf[i][j].push_back({i-1,j,(calc(mat[i][j])+4)%4});
if(j!=0)graf[i][j].push_back({i,j-1,(calc(mat[i][j])+3)%4});
if(i+1!=n)graf[i][j].push_back({i+1,j,(calc(mat[i][j])+2)%4});
if(j+1!=m)graf[i][j].push_back({i,j+1,(calc(mat[i][j])+1)%4});
}
}
dijkstra(0,0);
if(d[n-1][m-1]==INT_MAX)cout<<"-1\n";
else cout<<d[n-1][m-1]<<endl;
return 0;
}
Compilation message
adventure.cpp: In function 'int calc(char)':
adventure.cpp:44:1: warning: control reaches end of non-void function [-Wreturn-type]
44 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
4 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
4 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7340 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
4 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
4 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7340 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7268 KB |
Output is correct |
13 |
Correct |
3 ms |
7260 KB |
Output is correct |
14 |
Correct |
3 ms |
7260 KB |
Output is correct |
15 |
Correct |
3 ms |
7260 KB |
Output is correct |
16 |
Correct |
3 ms |
7260 KB |
Output is correct |
17 |
Correct |
3 ms |
7260 KB |
Output is correct |
18 |
Correct |
3 ms |
7260 KB |
Output is correct |
19 |
Correct |
3 ms |
7260 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
3 ms |
7332 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7256 KB |
Output is correct |
2 |
Correct |
4 ms |
7260 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7336 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
3 ms |
7276 KB |
Output is correct |
9 |
Correct |
3 ms |
7260 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
4 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7260 KB |
Output is correct |
7 |
Correct |
3 ms |
7260 KB |
Output is correct |
8 |
Correct |
4 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7340 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7268 KB |
Output is correct |
13 |
Correct |
3 ms |
7260 KB |
Output is correct |
14 |
Correct |
3 ms |
7260 KB |
Output is correct |
15 |
Correct |
3 ms |
7260 KB |
Output is correct |
16 |
Correct |
3 ms |
7260 KB |
Output is correct |
17 |
Correct |
3 ms |
7260 KB |
Output is correct |
18 |
Correct |
3 ms |
7260 KB |
Output is correct |
19 |
Correct |
3 ms |
7260 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
21 |
Correct |
3 ms |
7260 KB |
Output is correct |
22 |
Correct |
3 ms |
7260 KB |
Output is correct |
23 |
Correct |
3 ms |
7332 KB |
Output is correct |
24 |
Correct |
3 ms |
7260 KB |
Output is correct |
25 |
Correct |
5 ms |
7256 KB |
Output is correct |
26 |
Correct |
4 ms |
7260 KB |
Output is correct |
27 |
Correct |
3 ms |
7260 KB |
Output is correct |
28 |
Correct |
3 ms |
7260 KB |
Output is correct |
29 |
Correct |
3 ms |
7260 KB |
Output is correct |
30 |
Correct |
3 ms |
7336 KB |
Output is correct |
31 |
Correct |
3 ms |
7260 KB |
Output is correct |
32 |
Correct |
3 ms |
7276 KB |
Output is correct |
33 |
Correct |
3 ms |
7260 KB |
Output is correct |
34 |
Correct |
3 ms |
7260 KB |
Output is correct |
35 |
Correct |
9 ms |
8384 KB |
Output is correct |
36 |
Correct |
3 ms |
7260 KB |
Output is correct |
37 |
Correct |
9 ms |
8540 KB |
Output is correct |
38 |
Correct |
6 ms |
9564 KB |
Output is correct |
39 |
Correct |
78 ms |
20292 KB |
Output is correct |
40 |
Correct |
73 ms |
20136 KB |
Output is correct |
41 |
Correct |
24 ms |
20264 KB |
Output is correct |
42 |
Correct |
75 ms |
20308 KB |
Output is correct |
43 |
Correct |
65 ms |
23348 KB |
Output is correct |
44 |
Correct |
25 ms |
20316 KB |
Output is correct |