#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <limits.h>
/// W N E S
const std::vector<std::pair<int, int>> dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
struct Node {
int cost;
int x, y;
Node(int cost_, int x_, int y_) : cost(cost_), x(x_), y(y_) {}
bool operator < (const Node &other) const {
return cost < other.cost;
}
};
inline bool inside(int x, int y, int N, int M) {
return x >= 0 && y >= 0 && x < N && y < M;
}
int main() {
freopen("data.in", "r", stdin);
int N, M;
std::cin >> N >> M;
std::vector<std::string> mat(N);
for (auto &line : mat) {
std::cin >> line;
}
std::priority_queue<Node> Q;
Q.emplace(0, 0, 0);
std::vector<std::vector<int>> dist(N, std::vector<int>(M, INT_MAX / 2));
dist[0][0] = 0;
while (!Q.empty()) {
int cost = Q.top().cost, x = Q.top().x, y = Q.top().y;
Q.pop();
if (cost > dist[x][y]) {
continue;
}
int j = 0;
switch (mat[x][y]) {
case 'N':
j = 1;
break;
case 'E':
j = 2;
break;
case 'S':
j = 3;
break;
}
for (int i = 0; i < 4; i++) {
int nx = x + dirs[(i + j) % 4].first, ny = y + dirs[(i + j) % 4].second;
if (inside(nx, ny, N, M) && dist[nx][ny] > dist[x][y] + i) {
dist[nx][ny] = dist[x][y] + i;
if (mat[nx][ny] != 'X') {
Q.emplace(dist[nx][ny], nx, ny);
}
}
}
}
std::cout << (dist[N - 1][M - 1] < INT_MAX / 2 ? dist[N - 1][M - 1] : -1) << "\n";
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:24:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen("data.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
300 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |