#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1000;
#define pii pair<int,int>
#define pb push_back
struct Edge{
int x, y, w;
};
int n, m, dist[N][N];
char c;
vector<Edge> g[N][N];
bool vis[N][N];
int main(){
cin.tie(0); ios::sync_with_stdio(0);
cin >> n >> m;
int arr[4][2]={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
for(int i = 0; i <= N-1; i++) for(int j = 0; j <= N-1; j++) {dist[i][j] = 1e9; vis[i][j] = 0;}
Edge e;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c;
int co;
if(c == 'N'){
co = 0;
for(int k = 0; k < 4; k++){
e.x = i + arr[(k+co)%4][0];
e.y = j + arr[(k+co)%4][1];
e.w = k;
g[i][j].pb(e);
}
}else if(c == 'E'){
co=1;
for(int k = 0; k < 4; k++){
e.x = i + arr[(k+co)%4][0];
e.y = j + arr[(k+co)%4][1];
e.w = k;
g[i][j].pb(e);
}
}else if(c == 'S'){
co=2;
for(int k = 0; k < 4; k++){
e.x = i + arr[(k+co)%4][0];
e.y = j + arr[(k+co)%4][1];
e.w = k;
g[i][j].pb(e);
}
}else if(c == 'W'){
co=3;
for(int k = 0; k < 4; k++){
e.x = i + arr[(k+co)%4][0];
e.y = j + arr[(k+co)%4][1];
e.w = k;
g[i][j].pb(e);
}
}
}
}
dist[1][1] = 0;
priority_queue<pair<int, pii>> q;
q.push({0, {1, 1}});
while(!q.empty()){
int x = q.top().second.first, y = q.top().second.second;
q.pop();
if(vis[x][y]) continue;
vis[x][y] = 1;
for(Edge u: g[x][y]){
if(dist[x][y] + u.w < dist[u.x][u.y]){
dist[u.x][u.y] = dist[x][y] + u.w;
q.push({-dist[u.x][u.y], {u.x, u.y}});
}
}
}
cout << (!vis[n][m] ? -1 : dist[n][m]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28620 KB |
Output is correct |
2 |
Correct |
16 ms |
28612 KB |
Output is correct |
3 |
Correct |
16 ms |
28708 KB |
Output is correct |
4 |
Correct |
16 ms |
28620 KB |
Output is correct |
5 |
Correct |
16 ms |
28712 KB |
Output is correct |
6 |
Correct |
16 ms |
28620 KB |
Output is correct |
7 |
Correct |
16 ms |
28620 KB |
Output is correct |
8 |
Correct |
16 ms |
28620 KB |
Output is correct |
9 |
Correct |
16 ms |
28712 KB |
Output is correct |
10 |
Correct |
16 ms |
28620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28620 KB |
Output is correct |
2 |
Correct |
16 ms |
28612 KB |
Output is correct |
3 |
Correct |
16 ms |
28708 KB |
Output is correct |
4 |
Correct |
16 ms |
28620 KB |
Output is correct |
5 |
Correct |
16 ms |
28712 KB |
Output is correct |
6 |
Correct |
16 ms |
28620 KB |
Output is correct |
7 |
Correct |
16 ms |
28620 KB |
Output is correct |
8 |
Correct |
16 ms |
28620 KB |
Output is correct |
9 |
Correct |
16 ms |
28712 KB |
Output is correct |
10 |
Correct |
16 ms |
28620 KB |
Output is correct |
11 |
Correct |
16 ms |
28636 KB |
Output is correct |
12 |
Correct |
16 ms |
28664 KB |
Output is correct |
13 |
Correct |
16 ms |
28712 KB |
Output is correct |
14 |
Correct |
17 ms |
28680 KB |
Output is correct |
15 |
Correct |
16 ms |
28664 KB |
Output is correct |
16 |
Correct |
16 ms |
28588 KB |
Output is correct |
17 |
Correct |
16 ms |
28620 KB |
Output is correct |
18 |
Correct |
16 ms |
28584 KB |
Output is correct |
19 |
Correct |
16 ms |
28588 KB |
Output is correct |
20 |
Correct |
16 ms |
28620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28648 KB |
Output is correct |
2 |
Correct |
16 ms |
28620 KB |
Output is correct |
3 |
Correct |
16 ms |
28672 KB |
Output is correct |
4 |
Correct |
16 ms |
28620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28620 KB |
Output is correct |
2 |
Correct |
15 ms |
28588 KB |
Output is correct |
3 |
Correct |
16 ms |
28608 KB |
Output is correct |
4 |
Correct |
16 ms |
28636 KB |
Output is correct |
5 |
Correct |
16 ms |
28672 KB |
Output is correct |
6 |
Correct |
17 ms |
28656 KB |
Output is correct |
7 |
Correct |
16 ms |
28620 KB |
Output is correct |
8 |
Correct |
16 ms |
28676 KB |
Output is correct |
9 |
Correct |
16 ms |
28620 KB |
Output is correct |
10 |
Correct |
17 ms |
28620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28620 KB |
Output is correct |
2 |
Correct |
16 ms |
28612 KB |
Output is correct |
3 |
Correct |
16 ms |
28708 KB |
Output is correct |
4 |
Correct |
16 ms |
28620 KB |
Output is correct |
5 |
Correct |
16 ms |
28712 KB |
Output is correct |
6 |
Correct |
16 ms |
28620 KB |
Output is correct |
7 |
Correct |
16 ms |
28620 KB |
Output is correct |
8 |
Correct |
16 ms |
28620 KB |
Output is correct |
9 |
Correct |
16 ms |
28712 KB |
Output is correct |
10 |
Correct |
16 ms |
28620 KB |
Output is correct |
11 |
Correct |
16 ms |
28636 KB |
Output is correct |
12 |
Correct |
16 ms |
28664 KB |
Output is correct |
13 |
Correct |
16 ms |
28712 KB |
Output is correct |
14 |
Correct |
17 ms |
28680 KB |
Output is correct |
15 |
Correct |
16 ms |
28664 KB |
Output is correct |
16 |
Correct |
16 ms |
28588 KB |
Output is correct |
17 |
Correct |
16 ms |
28620 KB |
Output is correct |
18 |
Correct |
16 ms |
28584 KB |
Output is correct |
19 |
Correct |
16 ms |
28588 KB |
Output is correct |
20 |
Correct |
16 ms |
28620 KB |
Output is correct |
21 |
Correct |
17 ms |
28648 KB |
Output is correct |
22 |
Correct |
16 ms |
28620 KB |
Output is correct |
23 |
Correct |
16 ms |
28672 KB |
Output is correct |
24 |
Correct |
16 ms |
28620 KB |
Output is correct |
25 |
Correct |
16 ms |
28620 KB |
Output is correct |
26 |
Correct |
15 ms |
28588 KB |
Output is correct |
27 |
Correct |
16 ms |
28608 KB |
Output is correct |
28 |
Correct |
16 ms |
28636 KB |
Output is correct |
29 |
Correct |
16 ms |
28672 KB |
Output is correct |
30 |
Correct |
17 ms |
28656 KB |
Output is correct |
31 |
Correct |
16 ms |
28620 KB |
Output is correct |
32 |
Correct |
16 ms |
28676 KB |
Output is correct |
33 |
Correct |
16 ms |
28620 KB |
Output is correct |
34 |
Correct |
17 ms |
28620 KB |
Output is correct |
35 |
Correct |
25 ms |
29772 KB |
Output is correct |
36 |
Correct |
16 ms |
28796 KB |
Output is correct |
37 |
Correct |
25 ms |
29980 KB |
Output is correct |
38 |
Correct |
22 ms |
30792 KB |
Output is correct |
39 |
Correct |
113 ms |
41484 KB |
Output is correct |
40 |
Correct |
115 ms |
41496 KB |
Output is correct |
41 |
Correct |
57 ms |
41344 KB |
Output is correct |
42 |
Correct |
117 ms |
41476 KB |
Output is correct |
43 |
Correct |
143 ms |
47676 KB |
Output is correct |
44 |
Correct |
56 ms |
41412 KB |
Output is correct |