#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define v(a) vector<a>
#define p(a, b) pair<a, b>
#define F first
#define S second
ll n, m;
map<p(ll, ll), bool> vis;
map<char, ll> base;
v(v(char)) arr;
bool pos(ll x, ll y){
if(x < 0 || y < 0 || x >= n || y >= m) return false;
if(vis[{x, y}]) return false;
if(arr[x][y] == 'X' && x != n-1 && y != m-1) return false;
return true;
}
ll cost(ll a, ll b){
return ((b-a)+4)%4;
}
int main(){
cin >> n >> m;
arr.resize(n+5);
for(int i = 0; i < n; i++)arr[i].resize(m+5);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) cin >> arr[i][j];
}
vis.clear();
priority_queue<p(ll, p(ll, ll)), v(p(ll, p(ll, ll))), greater<p(ll, p(ll, ll))> > pq;
pq.push({0, {0, 0}});
base['N'] = 1;
base['E'] = 2;
base['S'] = 3;
base['W'] = 4;
ll ans = -1;
while(!pq.empty()){
ll cur = pq.top().F, x = pq.top().S.F, y = pq.top().S.S;
// cout << cur << " " << x << " " << y << endl;
pq.pop();
if(vis[{x, y}]) continue;
vis[{x, y}] = true;
if(x == n-1 && y == m-1){
ans = cur;
break;
}
if(arr[x][y] == 'X') continue;
if(pos(x+1, y)) pq.push({cur + cost(base[arr[x][y]], 3), {x+1, y}});
if(pos(x-1, y)) pq.push({cur + cost(base[arr[x][y]], 1), {x-1, y}});
if(pos(x, y+1)) pq.push({cur + cost(base[arr[x][y]], 2), {x, y+1}});
if(pos(x, y-1)) pq.push({cur + cost(base[arr[x][y]], 4), {x, y-1}});
}
cout << ans;
}
# |
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 |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
1 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 |
348 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 |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
17 ms |
1884 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
20 ms |
1924 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
214 ms |
16152 KB |
Output is correct |
40 |
Correct |
219 ms |
16252 KB |
Output is correct |
41 |
Correct |
9 ms |
600 KB |
Output is correct |
42 |
Correct |
220 ms |
16132 KB |
Output is correct |
43 |
Correct |
174 ms |
22000 KB |
Output is correct |
44 |
Correct |
8 ms |
604 KB |
Output is correct |