#include <iostream>
#include <utility>
#include <vector>
#include <tuple>
#include <queue>
using namespace std;
const int maxn = 505;
const int inf = 1000000007;
int n, m;
int table[maxn][maxn];
pair<int, int> dirs[4] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int timeForChange[4][4];
void MakeChangeTime() {
//W -> 0, N - 1, E - 2, S - 3
timeForChange[0][0] = 0;
timeForChange[0][1] = 1;
timeForChange[0][2] = 2;
timeForChange[0][3] = 3;
timeForChange[1][0] = 3;
timeForChange[1][1] = 0;
timeForChange[1][2] = 1;
timeForChange[1][3] = 2;
timeForChange[2][0] = 2;
timeForChange[2][1] = 3;
timeForChange[2][2] = 0;
timeForChange[2][3] = 1;
timeForChange[3][0] = 1;
timeForChange[3][1] = 2;
timeForChange[3][2] = 3;
timeForChange[3][3] = 0;
}
vector<pair<int, int>> minCoordinates[3 * maxn * maxn];
int dist[maxn][maxn];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c; cin >> c;
if (c == 'W') table[i][j] = 0;
if (c == 'N') table[i][j] = 1;
if (c == 'E') table[i][j] = 2;
if (c == 'S') table[i][j] = 3;
if (c == 'X') table[i][j] = -1;
}
}
if (table[1][1] == 'X') {
cout << -1 << endl;
return 0;
}
MakeChangeTime();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
dist[i][j] = inf;
}
}
dist[1][1] = 0;
minCoordinates[0].push_back({1, 1});
for (int path = 0; path <= 3 * n * m; ++path) {
for (int idx = 0; idx < minCoordinates[path].size(); idx++) {
int currX, currY;
tie(currX, currY) = minCoordinates[path][idx];
if (currX == n && currY == m) {
cout << path << endl;
exit(0);
}
if (dist[currX][currY] < path) continue;
if (table[currX][currY] == -1) continue;
for (int change = 0; change <= 3; change++) {
int newX = currX + dirs[change].first;
int newY = currY + dirs[change].second;
if (newX < 1 || newX > n || newY < 1 || newY > m) continue;
int currPath = path + timeForChange[table[currX][currY]][change];
if (currPath < dist[newX][newY]) {
dist[newX][newY] = currPath;
minCoordinates[currPath].push_back({newX, newY});
}
}
}
}
cout << -1 << endl;
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int idx = 0; idx < minCoordinates[path].size(); idx++) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18260 KB |
Output is correct |
2 |
Correct |
10 ms |
18296 KB |
Output is correct |
3 |
Correct |
10 ms |
18296 KB |
Output is correct |
4 |
Correct |
10 ms |
18260 KB |
Output is correct |
5 |
Correct |
9 ms |
18260 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
18276 KB |
Output is correct |
8 |
Correct |
10 ms |
18260 KB |
Output is correct |
9 |
Correct |
10 ms |
18292 KB |
Output is correct |
10 |
Correct |
9 ms |
18304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18260 KB |
Output is correct |
2 |
Correct |
10 ms |
18296 KB |
Output is correct |
3 |
Correct |
10 ms |
18296 KB |
Output is correct |
4 |
Correct |
10 ms |
18260 KB |
Output is correct |
5 |
Correct |
9 ms |
18260 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
18276 KB |
Output is correct |
8 |
Correct |
10 ms |
18260 KB |
Output is correct |
9 |
Correct |
10 ms |
18292 KB |
Output is correct |
10 |
Correct |
9 ms |
18304 KB |
Output is correct |
11 |
Correct |
9 ms |
18260 KB |
Output is correct |
12 |
Correct |
10 ms |
18260 KB |
Output is correct |
13 |
Correct |
10 ms |
18244 KB |
Output is correct |
14 |
Correct |
10 ms |
18260 KB |
Output is correct |
15 |
Correct |
10 ms |
18232 KB |
Output is correct |
16 |
Correct |
10 ms |
18260 KB |
Output is correct |
17 |
Correct |
9 ms |
18300 KB |
Output is correct |
18 |
Correct |
12 ms |
18204 KB |
Output is correct |
19 |
Correct |
10 ms |
18276 KB |
Output is correct |
20 |
Correct |
11 ms |
18316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18300 KB |
Output is correct |
2 |
Correct |
10 ms |
18300 KB |
Output is correct |
3 |
Correct |
11 ms |
18300 KB |
Output is correct |
4 |
Correct |
10 ms |
18260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
18260 KB |
Output is correct |
2 |
Correct |
10 ms |
18296 KB |
Output is correct |
3 |
Correct |
10 ms |
18300 KB |
Output is correct |
4 |
Correct |
10 ms |
18260 KB |
Output is correct |
5 |
Correct |
10 ms |
18292 KB |
Output is correct |
6 |
Correct |
11 ms |
18260 KB |
Output is correct |
7 |
Correct |
10 ms |
18260 KB |
Output is correct |
8 |
Correct |
11 ms |
18260 KB |
Output is correct |
9 |
Correct |
12 ms |
18348 KB |
Output is correct |
10 |
Correct |
10 ms |
18192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
18260 KB |
Output is correct |
2 |
Correct |
10 ms |
18296 KB |
Output is correct |
3 |
Correct |
10 ms |
18296 KB |
Output is correct |
4 |
Correct |
10 ms |
18260 KB |
Output is correct |
5 |
Correct |
9 ms |
18260 KB |
Output is correct |
6 |
Correct |
10 ms |
18260 KB |
Output is correct |
7 |
Correct |
9 ms |
18276 KB |
Output is correct |
8 |
Correct |
10 ms |
18260 KB |
Output is correct |
9 |
Correct |
10 ms |
18292 KB |
Output is correct |
10 |
Correct |
9 ms |
18304 KB |
Output is correct |
11 |
Correct |
9 ms |
18260 KB |
Output is correct |
12 |
Correct |
10 ms |
18260 KB |
Output is correct |
13 |
Correct |
10 ms |
18244 KB |
Output is correct |
14 |
Correct |
10 ms |
18260 KB |
Output is correct |
15 |
Correct |
10 ms |
18232 KB |
Output is correct |
16 |
Correct |
10 ms |
18260 KB |
Output is correct |
17 |
Correct |
9 ms |
18300 KB |
Output is correct |
18 |
Correct |
12 ms |
18204 KB |
Output is correct |
19 |
Correct |
10 ms |
18276 KB |
Output is correct |
20 |
Correct |
11 ms |
18316 KB |
Output is correct |
21 |
Correct |
10 ms |
18300 KB |
Output is correct |
22 |
Correct |
10 ms |
18300 KB |
Output is correct |
23 |
Correct |
11 ms |
18300 KB |
Output is correct |
24 |
Correct |
10 ms |
18260 KB |
Output is correct |
25 |
Correct |
9 ms |
18260 KB |
Output is correct |
26 |
Correct |
10 ms |
18296 KB |
Output is correct |
27 |
Correct |
10 ms |
18300 KB |
Output is correct |
28 |
Correct |
10 ms |
18260 KB |
Output is correct |
29 |
Correct |
10 ms |
18292 KB |
Output is correct |
30 |
Correct |
11 ms |
18260 KB |
Output is correct |
31 |
Correct |
10 ms |
18260 KB |
Output is correct |
32 |
Correct |
11 ms |
18260 KB |
Output is correct |
33 |
Correct |
12 ms |
18348 KB |
Output is correct |
34 |
Correct |
10 ms |
18192 KB |
Output is correct |
35 |
Correct |
12 ms |
18816 KB |
Output is correct |
36 |
Correct |
10 ms |
18260 KB |
Output is correct |
37 |
Correct |
12 ms |
19076 KB |
Output is correct |
38 |
Correct |
11 ms |
19328 KB |
Output is correct |
39 |
Correct |
28 ms |
23624 KB |
Output is correct |
40 |
Correct |
28 ms |
23640 KB |
Output is correct |
41 |
Correct |
19 ms |
20492 KB |
Output is correct |
42 |
Correct |
29 ms |
23604 KB |
Output is correct |
43 |
Correct |
25 ms |
25444 KB |
Output is correct |
44 |
Correct |
19 ms |
20436 KB |
Output is correct |