#include<bits/stdc++.h>
using namespace std;
#define p push
const int maxn = 507;
int kr[4]={-1,0,1,0};
int ks[4]={0,1,0,-1};
int n,m;
char c;
int sve[maxn][maxn];
int dji[maxn][maxn];
priority_queue<pair<int,pair<int,int>> ,vector<pair<int,pair<int,int>>> , greater<pair<int,pair<int,int>>>> q;
int main(){
cin>>n>>m;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
cin>>c;
if(c=='X'){
sve[i][j]=-1;
}
else if(c=='N'){
sve[i][j]=0;
}
else if(c=='E'){
sve[i][j]=1;
}
else if(c=='S'){
sve[i][j]=2;
}
else if(c=='W'){
sve[i][j]=3;
}
}
}
if(sve[0][0]==-1){
cout<<-1;
return 0;
}
sve[n-1][m-1]=-2;
q.p({0,{0,0}});
pair<int,pair<int,int>> tr;
bool ch = true;
while(!q.empty() and ch){
tr=q.top();
dji[tr.second.first][tr.second.second]=1;
if(tr.second.first==n-1 and tr.second.second==m-1){
cout<<tr.first;
ch=false;
break;
}
int pr = sve[tr.second.first][tr.second.second];
//cout<<tr.first<<" "<<tr.second.first<<' '<<tr.second.second<<" "<<pr<<endl;
q.pop();
for(int i = sve[tr.second.first][tr.second.second]; i<sve[tr.second.first][tr.second.second]+4; i++){
int r = tr.second.first +kr[i%4];
int s = tr.second.second + ks[i%4];
if(r>n-1 or s>m-1 or r<0 or s<0 or dji[r][s]!=0 or sve[r][s]==-1){
continue;
}
else{
//cout<<i<<" "<<r<<' '<<s<<endl;
q.p({tr.first+(i-pr),{r,s}});
}
}
}
if(ch) cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 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 |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 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 |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
448 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1760 ms |
51376 KB |
Output is correct |
6 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 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 |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
448 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1760 ms |
51376 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
12 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
17 ms |
856 KB |
Output is correct |
38 |
Correct |
2 ms |
856 KB |
Output is correct |
39 |
Correct |
294 ms |
4032 KB |
Output is correct |
40 |
Correct |
295 ms |
3784 KB |
Output is correct |
41 |
Correct |
9 ms |
1624 KB |
Output is correct |
42 |
Correct |
252 ms |
3768 KB |
Output is correct |
43 |
Correct |
34 ms |
5580 KB |
Output is correct |
44 |
Correct |
9 ms |
1628 KB |
Output is correct |