#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int maxn = 510;
const int inf = 1e9;
char a[maxn][maxn];
int d[maxn][maxn], n, m;
vector<int> g[maxn * maxn];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[i][j] = inf;
}
}
d[1][1] = 0;
priority_queue<pair<int, pair<int, int>>> pq;
pq.push({0, {1, 1}});
while(!pq.empty()) {
auto u = pq.top();
int c = u.f, x = u.s.f, y = u.s.s;
c = abs(c);
pq.pop();
if(a[x][y] == 'N') {
if(x - 1 >= 1 && d[x - 1][y] > c) {
d[x - 1][y] = c;
pq.push({-d[x - 1][y], {x - 1, y}});
}
if(y + 1 <= m && d[x][y + 1] > c + 1) {
d[x][y + 1] = c + 1;
pq.push({-d[x][y + 1], {x, y + 1}});
}
if(x + 1 <= n && d[x + 1][y] > c + 2) {
d[x + 1][y] = c + 2;
pq.push({-d[x + 1][y], {x + 1, y}});
}
if(y - 1 <= m && d[x][y - 1] > c + 3) {
d[x][y - 1] = c + 3;
pq.push({-d[x][y - 1], {x, y - 1}});
}
}
else if(a[x][y] == 'E') {
if(x - 1 >= 1 && d[x - 1][y] > c + 3) {
d[x - 1][y] = c + 3;
pq.push({-d[x - 1][y], {x - 1, y}});
}
if(y + 1 <= m && d[x][y + 1] > c) {
d[x][y + 1] = c;
pq.push({-d[x][y + 1], {x, y + 1}});
}
if(x + 1 <= n && d[x + 1][y] > c + 1) {
d[x + 1][y] = c + 1;
pq.push({-d[x + 1][y], {x + 1, y}});
}
if(y - 1 <= m && d[x][y - 1] > c + 2) {
d[x][y - 1] = c + 2;
pq.push({-d[x][y - 1], {x, y - 1}});
}
}
else if(a[x][y] == 'S') {
if(x - 1 >= 1 && d[x - 1][y] > c + 2) {
d[x - 1][y] = c + 2;
pq.push({-d[x - 1][y], {x - 1, y}});
}
if(y + 1 <= m && d[x][y + 1] > c + 3) {
d[x][y + 1] = c + 3;
pq.push({-d[x][y + 1], {x, y + 1}});
}
if(x + 1 <= n && d[x + 1][y] > c) {
d[x + 1][y] = c;
pq.push({-d[x + 1][y], {x + 1, y}});
}
if(y - 1 <= m && d[x][y - 1] > c + 1) {
d[x][y - 1] = c + 1;
pq.push({-d[x][y - 1], {x, y - 1}});
}
}
else if(a[x][y] == 'W') {
if(x - 1 >= 1 && d[x - 1][y] > c + 1) {
d[x - 1][y] = c + 1;
pq.push({-d[x - 1][y], {x - 1, y}});
}
if(y + 1 <= m && d[x][y + 1] > c + 2) {
d[x][y + 1] = c + 2;
pq.push({-d[x][y + 1], {x, y + 1}});
}
if(x + 1 <= n && d[x + 1][y] > c + 3) {
d[x + 1][y] = c + 3;
pq.push({-d[x + 1][y], {x + 1, y}});
}
if(y - 1 <= m && d[x][y - 1] > c) {
d[x][y - 1] = c;
pq.push({-d[x][y - 1], {x, y - 1}});
}
}
}
if(d[n][m] == inf)
cout << -1;
else
cout << d[n][m];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6440 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
3 ms |
6356 KB |
Output is correct |
5 |
Correct |
4 ms |
6356 KB |
Output is correct |
6 |
Correct |
3 ms |
6356 KB |
Output is correct |
7 |
Correct |
3 ms |
6440 KB |
Output is correct |
8 |
Correct |
3 ms |
6356 KB |
Output is correct |
9 |
Correct |
3 ms |
6356 KB |
Output is correct |
10 |
Correct |
3 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6440 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
3 ms |
6356 KB |
Output is correct |
5 |
Correct |
4 ms |
6356 KB |
Output is correct |
6 |
Correct |
3 ms |
6356 KB |
Output is correct |
7 |
Correct |
3 ms |
6440 KB |
Output is correct |
8 |
Correct |
3 ms |
6356 KB |
Output is correct |
9 |
Correct |
3 ms |
6356 KB |
Output is correct |
10 |
Correct |
3 ms |
6356 KB |
Output is correct |
11 |
Correct |
3 ms |
6356 KB |
Output is correct |
12 |
Correct |
3 ms |
6356 KB |
Output is correct |
13 |
Correct |
3 ms |
6356 KB |
Output is correct |
14 |
Correct |
3 ms |
6356 KB |
Output is correct |
15 |
Correct |
3 ms |
6444 KB |
Output is correct |
16 |
Correct |
3 ms |
6436 KB |
Output is correct |
17 |
Correct |
3 ms |
6356 KB |
Output is correct |
18 |
Correct |
3 ms |
6356 KB |
Output is correct |
19 |
Correct |
5 ms |
6560 KB |
Output is correct |
20 |
Correct |
3 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6356 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
4 ms |
6368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6356 KB |
Output is correct |
2 |
Correct |
3 ms |
6436 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
3 ms |
6356 KB |
Output is correct |
5 |
Correct |
3 ms |
6356 KB |
Output is correct |
6 |
Correct |
4 ms |
6436 KB |
Output is correct |
7 |
Correct |
3 ms |
6356 KB |
Output is correct |
8 |
Correct |
3 ms |
6356 KB |
Output is correct |
9 |
Correct |
3 ms |
6356 KB |
Output is correct |
10 |
Correct |
3 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6440 KB |
Output is correct |
3 |
Correct |
3 ms |
6356 KB |
Output is correct |
4 |
Correct |
3 ms |
6356 KB |
Output is correct |
5 |
Correct |
4 ms |
6356 KB |
Output is correct |
6 |
Correct |
3 ms |
6356 KB |
Output is correct |
7 |
Correct |
3 ms |
6440 KB |
Output is correct |
8 |
Correct |
3 ms |
6356 KB |
Output is correct |
9 |
Correct |
3 ms |
6356 KB |
Output is correct |
10 |
Correct |
3 ms |
6356 KB |
Output is correct |
11 |
Correct |
3 ms |
6356 KB |
Output is correct |
12 |
Correct |
3 ms |
6356 KB |
Output is correct |
13 |
Correct |
3 ms |
6356 KB |
Output is correct |
14 |
Correct |
3 ms |
6356 KB |
Output is correct |
15 |
Correct |
3 ms |
6444 KB |
Output is correct |
16 |
Correct |
3 ms |
6436 KB |
Output is correct |
17 |
Correct |
3 ms |
6356 KB |
Output is correct |
18 |
Correct |
3 ms |
6356 KB |
Output is correct |
19 |
Correct |
5 ms |
6560 KB |
Output is correct |
20 |
Correct |
3 ms |
6356 KB |
Output is correct |
21 |
Correct |
3 ms |
6356 KB |
Output is correct |
22 |
Correct |
4 ms |
6356 KB |
Output is correct |
23 |
Correct |
3 ms |
6356 KB |
Output is correct |
24 |
Correct |
4 ms |
6368 KB |
Output is correct |
25 |
Correct |
3 ms |
6356 KB |
Output is correct |
26 |
Correct |
3 ms |
6436 KB |
Output is correct |
27 |
Correct |
3 ms |
6356 KB |
Output is correct |
28 |
Correct |
3 ms |
6356 KB |
Output is correct |
29 |
Correct |
3 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6436 KB |
Output is correct |
31 |
Correct |
3 ms |
6356 KB |
Output is correct |
32 |
Correct |
3 ms |
6356 KB |
Output is correct |
33 |
Correct |
3 ms |
6356 KB |
Output is correct |
34 |
Correct |
3 ms |
6356 KB |
Output is correct |
35 |
Correct |
7 ms |
6708 KB |
Output is correct |
36 |
Correct |
4 ms |
6484 KB |
Output is correct |
37 |
Correct |
8 ms |
6964 KB |
Output is correct |
38 |
Correct |
4 ms |
7636 KB |
Output is correct |
39 |
Correct |
44 ms |
9004 KB |
Output is correct |
40 |
Correct |
47 ms |
9000 KB |
Output is correct |
41 |
Correct |
6 ms |
8916 KB |
Output is correct |
42 |
Correct |
44 ms |
9000 KB |
Output is correct |
43 |
Correct |
73 ms |
15164 KB |
Output is correct |
44 |
Correct |
6 ms |
8880 KB |
Output is correct |