#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 505;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m;
int d[N][N];
char a[N][N];
bool vis[N][N];
bool inside(int i, int j) {
return 1 <= i && i <= n && 1 <= j && j <= m && !vis[i][j];
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
map<char, int> mp = {{'N', 0}, {'E', 1}, {'S', 2}, {'W', 3}};
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
auto add = [&](int u, int i) {
if ((u += i) >= 4) {
u -= 4;
}
if (u < 0) {
u += 4;
}
return u;
};
memset(d, 0x3f, sizeof(d));
array<queue<array<int, 2>>, 4> q;
q[0].push({1, 1});
d[1][1] = 0;
int sz = 1, it = 0;
while (sz) {
while (!q[it].size()) {
it = add(it, 1);
}
auto [u, v] = q[it].front(); q[it].pop(); --sz;
if (vis[u][v]) {
continue;
}
vis[u][v] = 1;
if (a[u][v] != 'X') {
int p = mp[a[u][v]];
for (int dr = 0; dr < 4; ++dr) {
int x = u + dx[dr], y = v + dy[dr];
if (inside(x, y)) {
int w = add(dr, -p);
d[x][y] = min(d[x][y], d[u][v] + w);
q[add(it, w)].push({x, y}); ++sz;
}
}
}
}
cout << (d[n][m] < 1e9 ? d[n][m] : -1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1232 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1232 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1240 KB |
Output is correct |
16 |
Correct |
1 ms |
1432 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1376 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1376 KB |
Output is correct |
10 |
Correct |
1 ms |
1376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1232 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1240 KB |
Output is correct |
16 |
Correct |
1 ms |
1432 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1376 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
1 ms |
1376 KB |
Output is correct |
34 |
Correct |
1 ms |
1376 KB |
Output is correct |
35 |
Correct |
2 ms |
1376 KB |
Output is correct |
36 |
Correct |
1 ms |
1372 KB |
Output is correct |
37 |
Correct |
3 ms |
1564 KB |
Output is correct |
38 |
Correct |
1 ms |
1628 KB |
Output is correct |
39 |
Correct |
18 ms |
2084 KB |
Output is correct |
40 |
Correct |
18 ms |
2020 KB |
Output is correct |
41 |
Correct |
3 ms |
1880 KB |
Output is correct |
42 |
Correct |
18 ms |
2108 KB |
Output is correct |
43 |
Correct |
16 ms |
4256 KB |
Output is correct |
44 |
Correct |
3 ms |
1908 KB |
Output is correct |