#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
int n, m;
string s[500];
bool visited[500][500];
const int mvx[4] = {-1 , 0 , 1 , 0 };
const int mvy[4] = { 0 , 1 , 0 , -1 };
bool valid(int i, int j) {
return i >= 0 && j >= 0 && i < n && j < n && s[i][j];
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
priority_queue<tuple<unsigned int, int, int>, vector<tuple<unsigned int, int, int>>,
greater<tuple<unsigned int, int, int>>> pq;
pq.emplace(0, 0, 0);
int mv;
while (!pq.empty()) {
auto [moves, x, y] = pq.top(); pq.pop();
visited[x][y] = true;
if (s[x][y] == 'X') {
if (x == n - 1 && y == m - 1) {
cout << moves;
return 0;
}
continue;
}
switch (s[x][y]) {
case 'N': mv = 0; break;
case 'E': mv = 1; break;
case 'S': mv = 2; break;
case 'W': mv = 3;
}
for (int i = 0, r, c; i < 4; i++) {
r = x + mvx[i];
c = y + mvy[i];
if (valid(r, c) && !visited[r][c]) {
pq.emplace(moves + (i - mv + 4) % 4, r, c);
}
}
}
cout << -1;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:27:9: warning: 'mv' may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | int mv;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |