#include<bits/stdc++.h>
using namespace std;
#define int long long
int h[200005];
struct g{int x, y, t;};
bool cmp(g l, g r){
return l.t < r.t;
}
int df(char cur, char dir){
if(cur == dir) return 0;
if(cur == 'N') return 1 + df('E', dir);
if(cur == 'E') return 1 + df('S', dir);
if(cur == 'S') return 1 + df('W', dir);
if(cur == 'W') return 1 + df('N', dir);
return 0;
}
signed main(){
//ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<char>> a(n, vector<char>(m));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
}
vector<vector<int>> d(n, vector<int>(m, INT_MAX));
vector<vector<bool>> u(n, vector<bool>(m, false));
d[0][0] = 0;
multiset<g, decltype(&cmp)> pick(cmp);
pick.insert({0, 0, 0});
while(!pick.empty()){
//if(d[n-1][m-1] != INT_MAX) break;
int x, y;
g cur = *pick.begin();
pick.erase(pick.find(cur));
x = cur.x;
y = cur.y;
if(d[x][y] == INT_MAX) break;
if(u[x][y]) continue;
u[x][y] = true;
if(a[x][y] == 'X') continue;
if(x > 0) {
int tm = d[x][y] + df(a[x][y], 'N');
if(d[x-1][y] > tm){
d[x-1][y] = tm;
pick.insert({x-1, y, tm});
}
}
if(x < n-1){
int tm = d[x][y] + df(a[x][y], 'S');
if(d[x+1][y] > tm){
d[x+1][y] = tm;
pick.insert({ x+1, y, tm});
}
}
if(y > 0) {
int tm = d[x][y] + df(a[x][y], 'W');
if(d[x][y-1] > tm){
d[x][y-1] = tm;
pick.insert({ x, y-1, tm});
}
}
if(y < m-1) {
int tm = d[x][y] + df(a[x][y], 'E');
if(d[x][y+1] > tm){
d[x][y+1] = tm;
pick.insert({x, y+1, tm});
}
}
}
if(d[n-1][m-1] == INT_MAX) cout << "-1";
else cout << d[n-1][m-1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
396 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
396 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
508 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
396 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
508 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
7 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
9 ms |
596 KB |
Output is correct |
38 |
Correct |
3 ms |
708 KB |
Output is correct |
39 |
Correct |
70 ms |
3020 KB |
Output is correct |
40 |
Correct |
78 ms |
2900 KB |
Output is correct |
41 |
Correct |
10 ms |
2900 KB |
Output is correct |
42 |
Correct |
80 ms |
3020 KB |
Output is correct |
43 |
Correct |
121 ms |
18252 KB |
Output is correct |
44 |
Correct |
15 ms |
2900 KB |
Output is correct |