#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int nmax = 500;
const int dir = 4;
const int inf = 1e9;
int n, m;
char a[nmax + 2][nmax + 2];
int dl[dir] = { -1, 0, 1, 0 };
int dc[dir] = { 0, 1, 0, -1 };
int cod[128];
int dist[nmax * nmax + 1];
int vis[nmax * nmax + 1];
vector < pair < int, int > > g[nmax * nmax + 1];
void dijkstra ( int start ) {
priority_queue < pair < int, int > > pq;
for ( int i = 0; i < n * m; i++ )
dist[i] = inf;
dist[0] = 0;
pq.push ( { 0, start } );
while ( ! pq.empty() ) {
int node = pq.top ().second;
pq.pop ();
if ( vis[node] == false ) {
vis[node] = true;
for ( auto x: g[node] )
if ( dist[node] + x.second < dist[x.first] ) {
dist[x.first] = dist[node] + x.second;
pq.push ( { -dist[x.first], x.first } );
}
}
}
}
static inline void add ( int x, int y, int d ) {
if ( 0 <= x && x < n * m && 0 <= y && y < n * m )
g[x].push_back ( { y, d } );
}
static inline int lin ( int x, int y ) {
if ( x < 0 || x >= n || y < 0 || y >= m )
return -1;
return x * m + y;
}
int vec[4];
int main () {
cin >> n >> m;
for ( int i = 0; i < n; i++ )
cin >> a[i];
cod['N'] = 0; cod['E'] = 1; cod['S'] = 2; cod['W'] = 3;
cod['X'] = -1;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j < m; j++ )
if ( a[i][j] != 'X' ) {
int curr = lin ( i, j ), x;
for ( int d = 0; d < dir; d++ )
vec[d] = lin ( i + dl[d], j + dc[d] );
for ( int k = 0; k < 4; k++ )
if ( ( x = vec[( cod[a[i][j]] + k ) % dir] ) != -1 )
add ( curr, x , k );
}
dijkstra ( 0 );
int r = dist[lin ( n - 1, m - 1 )];
cout << ( r == inf? -1 : r );
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:59:48: warning: array subscript has type 'char' [-Wchar-subscripts]
59 | if ( ( x = vec[( cod[a[i][j]] + k ) % dir] ) != -1 )
| ~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6100 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6100 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
3 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
6100 KB |
Output is correct |
15 |
Correct |
4 ms |
6100 KB |
Output is correct |
16 |
Correct |
3 ms |
6100 KB |
Output is correct |
17 |
Correct |
3 ms |
6100 KB |
Output is correct |
18 |
Correct |
3 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6132 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6228 KB |
Output is correct |
6 |
Correct |
4 ms |
6228 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6100 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6100 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
3 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
6100 KB |
Output is correct |
15 |
Correct |
4 ms |
6100 KB |
Output is correct |
16 |
Correct |
3 ms |
6100 KB |
Output is correct |
17 |
Correct |
3 ms |
6100 KB |
Output is correct |
18 |
Correct |
3 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
21 |
Correct |
3 ms |
6100 KB |
Output is correct |
22 |
Correct |
3 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
6132 KB |
Output is correct |
24 |
Correct |
3 ms |
6100 KB |
Output is correct |
25 |
Correct |
3 ms |
6100 KB |
Output is correct |
26 |
Correct |
3 ms |
6100 KB |
Output is correct |
27 |
Correct |
3 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
6100 KB |
Output is correct |
29 |
Correct |
3 ms |
6228 KB |
Output is correct |
30 |
Correct |
4 ms |
6228 KB |
Output is correct |
31 |
Correct |
3 ms |
6100 KB |
Output is correct |
32 |
Correct |
3 ms |
6100 KB |
Output is correct |
33 |
Correct |
3 ms |
6100 KB |
Output is correct |
34 |
Correct |
3 ms |
6180 KB |
Output is correct |
35 |
Correct |
10 ms |
7216 KB |
Output is correct |
36 |
Correct |
5 ms |
6228 KB |
Output is correct |
37 |
Correct |
11 ms |
7380 KB |
Output is correct |
38 |
Correct |
10 ms |
8020 KB |
Output is correct |
39 |
Correct |
78 ms |
17964 KB |
Output is correct |
40 |
Correct |
87 ms |
18012 KB |
Output is correct |
41 |
Correct |
38 ms |
16972 KB |
Output is correct |
42 |
Correct |
76 ms |
18008 KB |
Output is correct |
43 |
Correct |
95 ms |
22280 KB |
Output is correct |
44 |
Correct |
36 ms |
16972 KB |
Output is correct |