#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ull unsigned ll
#define pb push_back
#define epb emplace_back
#define INF 1e9+1;
using namespace std;
vector<vector<pii> >g(250001);
int n,m;
int conv(int r,int c){
return r*m+c;
}
char c[501][501];
void solve(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>c[i][j];
if(c[i][j]=='S'){
if(i+1<n){
g[conv(i,j)].epb(conv(i+1,j),0);
}
if(j+1<m){
g[conv(i,j)].epb(conv(i,j+1),3);
}
if(i-1>=0){
g[conv(i,j)].epb(conv(i-1,j),2);
}
if(j-1>=0){
g[conv(i,j)].epb(conv(i,j-1),1);
}
}
if(c[i][j]=='E'){
if(j+1<m){
g[conv(i,j)].epb(conv(i,j+1),0);
}
if(i-1>=0){
g[conv(i,j)].epb(conv(i-1,j),3);
}
if(j-1>=0){
g[conv(i,j)].epb(conv(i,j-1),2);
}
if(i+1<n){
g[conv(i,j)].epb(conv(i+1,j),1);
}
}
if(c[i][j]=='N'){
if(i-1>=0){
g[conv(i,j)].epb(conv(i-1,j),0);
}
if(j-1>=0){
g[conv(i,j)].epb(conv(i,j-1),3);
}
if(i+1<n){
g[conv(i,j)].epb(conv(i+1,j),2);
}
if(j+1<m){
g[conv(i,j)].epb(conv(i,j+1),1);
}
}
if(c[i][j]=='W'){
if(j-1>=0){
g[conv(i,j)].epb(conv(i,j-1),0);
}
if(i+1<n){
g[conv(i,j)].epb(conv(i+1,j),3);
}
if(j+1<m){
g[conv(i,j)].epb(conv(i,j+1),2);
}
if(i-1>=0){
g[conv(i,j)].epb(conv(i-1,j),1);
}
}
}
}
int s=0,f=conv(n-1,m-1);
vector<int>d(500001,1e9+1);
d[s]=0;
set<pii>q;
q.insert({0,s});
while(!q.empty()){
int v=q.begin()->s;
q.erase(q.begin());
for(pii k:g[v]){
int to=k.f,len=k.s;
if(d[to]>d[v]+len){
q.erase({d[to],to});
d[to]=d[v]+len;
q.insert({d[to],to});
}
}
}
if(d[f]==1e9+1)cout<<-1;
else cout<<d[f];
}
int main(){
int t;t=1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8212 KB |
Output is correct |
2 |
Correct |
5 ms |
8104 KB |
Output is correct |
3 |
Correct |
5 ms |
8108 KB |
Output is correct |
4 |
Correct |
5 ms |
8108 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8116 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8212 KB |
Output is correct |
2 |
Correct |
5 ms |
8104 KB |
Output is correct |
3 |
Correct |
5 ms |
8108 KB |
Output is correct |
4 |
Correct |
5 ms |
8108 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8116 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8104 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
5 ms |
8108 KB |
Output is correct |
15 |
Correct |
5 ms |
8108 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
6 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8064 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8112 KB |
Output is correct |
2 |
Correct |
5 ms |
8104 KB |
Output is correct |
3 |
Correct |
6 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
6 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
6 ms |
8108 KB |
Output is correct |
7 |
Correct |
6 ms |
8124 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
6 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8212 KB |
Output is correct |
2 |
Correct |
5 ms |
8104 KB |
Output is correct |
3 |
Correct |
5 ms |
8108 KB |
Output is correct |
4 |
Correct |
5 ms |
8108 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8116 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8104 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
5 ms |
8108 KB |
Output is correct |
15 |
Correct |
5 ms |
8108 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
6 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8064 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8112 KB |
Output is correct |
22 |
Correct |
5 ms |
8104 KB |
Output is correct |
23 |
Correct |
6 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8104 KB |
Output is correct |
25 |
Correct |
6 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8140 KB |
Output is correct |
30 |
Correct |
6 ms |
8108 KB |
Output is correct |
31 |
Correct |
6 ms |
8124 KB |
Output is correct |
32 |
Correct |
5 ms |
8140 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
6 ms |
8140 KB |
Output is correct |
35 |
Correct |
15 ms |
9036 KB |
Output is correct |
36 |
Correct |
6 ms |
8236 KB |
Output is correct |
37 |
Correct |
17 ms |
9148 KB |
Output is correct |
38 |
Correct |
15 ms |
9804 KB |
Output is correct |
39 |
Correct |
123 ms |
18048 KB |
Output is correct |
40 |
Correct |
120 ms |
18048 KB |
Output is correct |
41 |
Correct |
54 ms |
17988 KB |
Output is correct |
42 |
Correct |
118 ms |
17984 KB |
Output is correct |
43 |
Correct |
128 ms |
20320 KB |
Output is correct |
44 |
Correct |
55 ms |
17964 KB |
Output is correct |