#include<bits/stdc++.h>
using namespace std;
#define MAX 507
vector < vector < int >> g(MAX, vector < int > (MAX, 0));
vector < vector < int >> dist(MAX, vector < int > (MAX, -1));
int dx[4] = {
0,
1,
0 - 1
}, dy[4] = {
-1,
0,
1,
0
}, n, m;
void dj() {
priority_queue < pair < int, pair < int, int >> , vector < pair < int, pair < int, int >> > , greater < pair < int, pair < int, int > > > > pq;
pq.push(make_pair(0, make_pair(0, 0)));
while (!pq.empty()) {
int x = pq.top().second.first, y = pq.top().second.second, c = pq.top().first;
pq.pop();
if (x < 0 || x >= m || y < 0 || y >= n || dist[y][x] != -1) continue;
dist[y][x] = c;
if (g[y][x] == -1) continue;
for (int i = 0; i < 4; i++) pq.push(make_pair(c + i, make_pair(x + dx[(g[y][x] + i) % 4], y + dy[(g[y][x] + i) % 4])));
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == 'N') g[i][j] = 0;
if (c == 'E') g[i][j] = 1;
if (c == 'S') g[i][j] = 2;
if (c == 'W') g[i][j] = 3;
else if (c == 'X') g[i][j] = -1;
}
}
dj();
cout << dist[n - 1][m - 1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2232 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2252 KB |
Output is correct |
10 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2232 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2252 KB |
Output is correct |
10 |
Correct |
2 ms |
2252 KB |
Output is correct |
11 |
Correct |
2 ms |
2252 KB |
Output is correct |
12 |
Correct |
2 ms |
2252 KB |
Output is correct |
13 |
Correct |
2 ms |
2252 KB |
Output is correct |
14 |
Correct |
2 ms |
2252 KB |
Output is correct |
15 |
Correct |
2 ms |
2252 KB |
Output is correct |
16 |
Correct |
2 ms |
2252 KB |
Output is correct |
17 |
Correct |
2 ms |
2252 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2252 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2232 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2252 KB |
Output is correct |
10 |
Correct |
2 ms |
2252 KB |
Output is correct |
11 |
Correct |
2 ms |
2252 KB |
Output is correct |
12 |
Correct |
2 ms |
2252 KB |
Output is correct |
13 |
Correct |
2 ms |
2252 KB |
Output is correct |
14 |
Correct |
2 ms |
2252 KB |
Output is correct |
15 |
Correct |
2 ms |
2252 KB |
Output is correct |
16 |
Correct |
2 ms |
2252 KB |
Output is correct |
17 |
Correct |
2 ms |
2252 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2252 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Incorrect |
2 ms |
2252 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |