#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e2 + 5;
char grid[N][N];
int n, m;
vector < vector < bool > > vis ( N, vector < bool > ( N, 0));
priority_queue < pair < int, pair < int, int >>, vector < pair < int, pair < int, int >>>, greater < pair < int, pair < int, int >>>> pq;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for ( int i = 1; i <= n; i++){
for ( int j = 1; j <= m; j++){
cin >> grid[i][j];
}
}
pq.push({0, {1, 1}});
while ( !pq.empty()){
int x = pq.top().second.first, y = pq.top().second.second, val = pq.top().first;
pq.pop();
if ( x == n && y == m ){
cout << val << endl;
return 0;
}
if ( vis[x][y] ) continue; vis[x][y] = 1;
if ( grid[x][y] == 'N'){
if ( x - 1 >= 1 && !vis[x-1][y] && (( grid[x - 1][y] != 'X' && !( x - 1 == n && y == m )) || ( x - 1 == n && y == m )) ) pq.push({val, {x - 1, y}});
if ( y + 1 <= m && !vis[x][y + 1] && (( grid[x][y+1] != 'X' && !( x == n && y + 1 == m )) || ( x == n && y + 1 == m)) ) pq.push({val + 1, {x, y + 1}});
if ( x + 1 <= n && !vis[x + 1][y] && (( grid[x + 1][y] != 'X' && !( x + 1 == n && y == m )) || ( x + 1 == n && y == m)) ) pq.push({val + 2, {x + 1, y}});
if ( y - 1 >= 1 && !vis[x][y - 1] && (( grid[x][y - 1] != 'X' && !( x == n && y - 1 == m )) || ( x == n && y - 1 == m )) ) pq.push({val + 3, {x, y - 1}});
}
else if ( grid[x][y] == 'E'){
if ( x - 1 >= 1 && !vis[x-1][y] && (( grid[x - 1][y] != 'X' && !( x - 1 == n && y == m )) || ( x - 1 == n && y == m )) ) pq.push({val + 3, {x - 1, y}});
if ( y + 1 <= m && !vis[x][y + 1]&& (( grid[x][y+1] != 'X' && !( x == n && y + 1 == m )) || ( x == n && y + 1 == m))) pq.push({val, {x, y + 1}});
if ( x + 1 <= n && !vis[x + 1][y]&& (( grid[x + 1][y] != 'X' && !( x + 1 == n && y == m )) || ( x + 1 == n && y == m))) pq.push({val + 1, {x + 1, y}});
if ( y - 1 >= 1 && !vis[x][y - 1] && (( grid[x][y - 1] != 'X' && !( x == n && y - 1 == m )) || ( x == n && y - 1 == m ))) pq.push({val + 2, {x, y - 1}});
}
else if ( grid[x][y] == 'S'){
if ( x - 1 >= 1 && !vis[x-1][y]&& (( grid[x - 1][y] != 'X' && !( x - 1 == n && y == m )) || ( x - 1 == n && y == m ))) pq.push({val + 2, {x - 1, y}});
if ( y + 1 <= m && !vis[x][y + 1]&& (( grid[x][y+1] != 'X' && !( x == n && y + 1 == m )) || ( x == n && y + 1 == m))) pq.push({val + 3, {x, y + 1}});
if ( x + 1 <= n && !vis[x + 1][y]&& (( grid[x + 1][y] != 'X' && !( x + 1 == n && y == m )) || ( x + 1 == n && y == m))) pq.push({val, {x + 1, y}});
if ( y - 1 >= 1 && !vis[x][y - 1]&& (( grid[x][y - 1] != 'X' && !( x == n && y - 1 == m )) || ( x == n && y - 1 == m )) ) pq.push({val + 1, {x, y - 1}});
}
else if ( grid[x][y] == 'W'){
if ( x - 1 >= 1 && !vis[x-1][y]&& (( grid[x - 1][y] != 'X' && !( x - 1 == n && y == m )) || ( x - 1 == n && y == m ))) pq.push({val + 1, {x - 1, y}});
if ( y + 1 <= m && !vis[x][y + 1]&& (( grid[x][y+1] != 'X' && !( x == n && y + 1 == m )) || ( x == n && y + 1 == m))) pq.push({val + 2, {x, y + 1}});
if ( x + 1 <= n && !vis[x + 1][y]&& (( grid[x + 1][y] != 'X' && !( x + 1 == n && y == m )) || ( x + 1 == n && y == m))) pq.push({val + 3, {x + 1, y}});
if ( y - 1 >= 1 && !vis[x][y - 1]&& (( grid[x][y - 1] != 'X' && !( x == n && y - 1 == m )) || ( x == n && y - 1 == m ))) pq.push({val, {x, y - 1}});
}
}
cout << -1 << endl;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:37:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
37 | if ( vis[x][y] ) continue; vis[x][y] = 1;
| ^~
adventure.cpp:37:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
37 | if ( vis[x][y] ) continue; vis[x][y] = 1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
412 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
412 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
412 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
4 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
5 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
47 ms |
808 KB |
Output is correct |
40 |
Correct |
47 ms |
604 KB |
Output is correct |
41 |
Correct |
3 ms |
600 KB |
Output is correct |
42 |
Correct |
46 ms |
600 KB |
Output is correct |
43 |
Correct |
41 ms |
4848 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |