#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=505,mod=1e9+7;
int n,m,x,y[5],d[N*N]; char ch;
vector <pair<int,int> > v[N*N];
set <pair<int,int> > s;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=0; i<n; i++)
for (int j=0; j<m; j++){
cin>>ch; d[i*m+j]=1e18;
if (ch=='N'){
y[0]=3; y[1]=1;
y[2]=0; y[3]=2;
}
if (ch=='S'){
y[0]=1; y[1]=3;
y[2]=2; y[3]=0;
}
if (ch=='E'){
y[0]=2; y[1]=0;
y[2]=3; y[3]=1;
}
if (ch=='W'){
y[0]=0; y[1]=2;
y[2]=1; y[3]=3;
}
if (ch!='X'){
if (j>0) v[i*m+j].push_back({i*m+j-1,y[0]});
if (j<m-1) v[i*m+j].push_back({i*m+j+1,y[1]});
if (i>0) v[i*m+j].push_back({(i-1)*m+j,y[2]});
if (i<n-1) v[i*m+j].push_back({(i+1)*m+j,y[3]});
}
}
s.insert({0,0}); d[0]=0;
while (!s.empty()){
x=s.begin()->second; s.erase(s.begin());
for (auto i:v[x]){
if (d[i.first]>d[x]+i.second){
s.erase({d[i.first],i.first});
d[i.first]=d[x]+i.second;
s.insert({d[i.first],i.first});
}
}
}
cout<<(d[n*m-1]==1e18?-1:d[n*m-1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6792 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6792 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6792 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6748 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
1 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
6 ms |
8028 KB |
Output is correct |
36 |
Correct |
1 ms |
6748 KB |
Output is correct |
37 |
Correct |
7 ms |
8284 KB |
Output is correct |
38 |
Correct |
4 ms |
9308 KB |
Output is correct |
39 |
Correct |
62 ms |
24392 KB |
Output is correct |
40 |
Correct |
62 ms |
24156 KB |
Output is correct |
41 |
Correct |
22 ms |
24156 KB |
Output is correct |
42 |
Correct |
61 ms |
24156 KB |
Output is correct |
43 |
Correct |
61 ms |
28140 KB |
Output is correct |
44 |
Correct |
22 ms |
24144 KB |
Output is correct |