#pragma GCC optimize("Ofast")
#include"bits/stdc++.h"
#define int long long
using namespace std;
const int sz = 5e2 + 10;
vector<vector<int>> aj[sz * sz];
char ad[] = {'N', 'S', 'W', 'E'};
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dis[sz * sz];
bool vi[sz * sz];
char g[sz][sz];
int m, n;
bool leg(int i, int j) {
return 0 < i && 0 < j && i <= m && j <= n;
}
void dij(int i) {
priority_queue<vector<int>> pq;
pq.push({0, i});
while (!pq.empty()) {
vector<int> f = pq.top();
pq.pop();
if (vi[f[1]]) continue;
for (vector<int> u : aj[f[1]]) {
if (vi[u[1]]) continue;
pq.push({f[0] + u[0], u[1]});
dis[u[1]] = max(dis[u[1]], f[0] + u[0]);
// if (u[1] == 1506) {
// cout << dis[u[1]] << ' ' << f[1] << ' ' << f[0] << ' ' << u[0] << '\n';
// }
}
vi[f[1]] = true;
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
map <char, int> cd;
cd['W'] = 1, cd['N'] = 2, cd['E'] = 3, cd['S'] = 4;
cin >> m >> n;
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
cin >> g[i][j];
}
}
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++) {
if ('X' == g[i][j]) continue;
for (int u = 0; u < 4; u ++) {
int dxx = i + dx[u];
int dyy = j + dy[u];
if (leg(dxx, dyy)) {
// int ds = (cd[g[i][j]] + 4 - cd[ad[u]]) % 4;
int ds = 0;
if (cd[g[i][j]] <= cd[ad[u]]) {
ds = cd[ad[u]] - cd[g[i][j]];
} else {
// cout << cd[a[u]] << ' ' << cd[g[i][j]] ;
ds = 4 - cd[g[i][j]] + cd[ad[u]];
}
// cout << i * 501 + j << ' ' << dxx * 501 + dyy << ' ' << ds << ' ' << g[i][j] << ' ' << ad[u] << '\n';
aj[i * 501 + j].push_back({-ds, dxx * 501 + dyy});
}
}
}
}
fill_n(dis, m * 501 + n + 10, INT_MIN);
dij(502);
int r = dis[m * 501 + n];
if (r == INT_MIN) {
cout << "-1\n";
return 0;
}
cout << -r << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8808 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8808 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8808 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
9052 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
9052 KB |
Output is correct |
30 |
Correct |
2 ms |
9052 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
2 ms |
8792 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
15 ms |
13260 KB |
Output is correct |
36 |
Correct |
3 ms |
9308 KB |
Output is correct |
37 |
Correct |
18 ms |
13828 KB |
Output is correct |
38 |
Correct |
12 ms |
16728 KB |
Output is correct |
39 |
Correct |
180 ms |
56564 KB |
Output is correct |
40 |
Correct |
167 ms |
56144 KB |
Output is correct |
41 |
Correct |
59 ms |
55892 KB |
Output is correct |
42 |
Correct |
168 ms |
56148 KB |
Output is correct |
43 |
Correct |
191 ms |
81056 KB |
Output is correct |
44 |
Correct |
60 ms |
55888 KB |
Output is correct |