#include <stdio.h>
#include <queue>
#include <vector>
#include <ctype.h>
#define MAX_N 500
#define DIR 4
using namespace std;
struct muchie {
int nod, cost;
bool operator < (const muchie &aux) const {
return cost > aux.cost;
}
};
int dist[MAX_N * MAX_N], dir[MAX_N + 2][MAX_N + 2];
int dl[DIR] = { -1, 0, 1, 0 }, dc[DIR] = { 0, 1, 0, -1 };
vector <muchie> muchii[MAX_N * MAX_N];
priority_queue <muchie> pq;
char nextChar() {
char ch;
ch = getc( stdin );
while ( !isalpha( ch ) )
ch = getc( stdin );
return ch;
}
int main() {
char ch;
int n, m, l, c, d, i;
struct muchie crt, next;
scanf( "%d%d", &n, &m );
for ( l = 1; l <= n; l++ ) {
for ( c = 1; c <= m; c++ ) {
ch = nextChar();
if ( ch == 'N' )
dir[l][c] = 0;
else if ( ch == 'E' )
dir[l][c] = 1;
else if ( ch == 'S' )
dir[l][c] = 2;
else if ( ch == 'W' )
dir[l][c] = 3;
else
dir[l][c] = 4;
}
}
for ( l = 1; l <= n; l++ )
dir[l][0] = dir[l][m + 1] = -1;
for ( c = 1; c <= m; c++ )
dir[0][c] = dir[n + 1][c] = -1;
for ( l = 1; l <= n; l++ ) {
for ( c = 1; c <= m; c++ ) {
if ( dir[l][c] < 4 ) {
for ( d = 0; d < DIR; d++ ) {
if ( dir[l + dl[d]][c + dc[d]] >= 0 )
muchii[(l - 1) * m + c - 1].push_back( { (l - 1 + dl[d]) * m + c - 1 + dc[d], (d - dir[l][c] + DIR) % DIR } );
}
}
}
}
for ( i = 0; i < n * m; i++ )
dist[i] = -1;
pq.push( { 0, 0 } );
while ( !pq.empty() ) {
crt = pq.top();
pq.pop();
if ( dist[crt.nod] == -1 ) {
dist[crt.nod] = crt.cost;
for ( i = 0; i < muchii[crt.nod].size(); i++ ) {
next = muchii[crt.nod][i];
if ( dist[next.nod] == -1 )
pq.push( { next.nod, dist[crt.nod] + next.cost } );
}
}
}
printf( "%d ", dist[n * m - 1] );
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:81:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<muchie>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for ( i = 0; i < muchii[crt.nod].size(); i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf( "%d%d", &n, &m );
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
4 ms |
6156 KB |
Output is correct |
4 |
Correct |
4 ms |
6092 KB |
Output is correct |
5 |
Correct |
4 ms |
6156 KB |
Output is correct |
6 |
Correct |
4 ms |
6092 KB |
Output is correct |
7 |
Correct |
5 ms |
6148 KB |
Output is correct |
8 |
Correct |
4 ms |
6092 KB |
Output is correct |
9 |
Correct |
4 ms |
6092 KB |
Output is correct |
10 |
Correct |
4 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
4 ms |
6156 KB |
Output is correct |
4 |
Correct |
4 ms |
6092 KB |
Output is correct |
5 |
Correct |
4 ms |
6156 KB |
Output is correct |
6 |
Correct |
4 ms |
6092 KB |
Output is correct |
7 |
Correct |
5 ms |
6148 KB |
Output is correct |
8 |
Correct |
4 ms |
6092 KB |
Output is correct |
9 |
Correct |
4 ms |
6092 KB |
Output is correct |
10 |
Correct |
4 ms |
6092 KB |
Output is correct |
11 |
Correct |
4 ms |
6092 KB |
Output is correct |
12 |
Correct |
4 ms |
6092 KB |
Output is correct |
13 |
Correct |
4 ms |
6092 KB |
Output is correct |
14 |
Correct |
5 ms |
6108 KB |
Output is correct |
15 |
Correct |
4 ms |
6092 KB |
Output is correct |
16 |
Correct |
5 ms |
6092 KB |
Output is correct |
17 |
Correct |
4 ms |
6092 KB |
Output is correct |
18 |
Correct |
4 ms |
6092 KB |
Output is correct |
19 |
Correct |
4 ms |
6092 KB |
Output is correct |
20 |
Correct |
4 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
4 ms |
6156 KB |
Output is correct |
4 |
Correct |
5 ms |
6104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
4 ms |
6092 KB |
Output is correct |
4 |
Correct |
4 ms |
6092 KB |
Output is correct |
5 |
Correct |
6 ms |
6092 KB |
Output is correct |
6 |
Correct |
4 ms |
6092 KB |
Output is correct |
7 |
Correct |
4 ms |
6092 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6092 KB |
Output is correct |
10 |
Correct |
4 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
4 ms |
6092 KB |
Output is correct |
3 |
Correct |
4 ms |
6156 KB |
Output is correct |
4 |
Correct |
4 ms |
6092 KB |
Output is correct |
5 |
Correct |
4 ms |
6156 KB |
Output is correct |
6 |
Correct |
4 ms |
6092 KB |
Output is correct |
7 |
Correct |
5 ms |
6148 KB |
Output is correct |
8 |
Correct |
4 ms |
6092 KB |
Output is correct |
9 |
Correct |
4 ms |
6092 KB |
Output is correct |
10 |
Correct |
4 ms |
6092 KB |
Output is correct |
11 |
Correct |
4 ms |
6092 KB |
Output is correct |
12 |
Correct |
4 ms |
6092 KB |
Output is correct |
13 |
Correct |
4 ms |
6092 KB |
Output is correct |
14 |
Correct |
5 ms |
6108 KB |
Output is correct |
15 |
Correct |
4 ms |
6092 KB |
Output is correct |
16 |
Correct |
5 ms |
6092 KB |
Output is correct |
17 |
Correct |
4 ms |
6092 KB |
Output is correct |
18 |
Correct |
4 ms |
6092 KB |
Output is correct |
19 |
Correct |
4 ms |
6092 KB |
Output is correct |
20 |
Correct |
4 ms |
6092 KB |
Output is correct |
21 |
Correct |
4 ms |
6092 KB |
Output is correct |
22 |
Correct |
4 ms |
6092 KB |
Output is correct |
23 |
Correct |
4 ms |
6156 KB |
Output is correct |
24 |
Correct |
5 ms |
6104 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
4 ms |
6092 KB |
Output is correct |
27 |
Correct |
4 ms |
6092 KB |
Output is correct |
28 |
Correct |
4 ms |
6092 KB |
Output is correct |
29 |
Correct |
6 ms |
6092 KB |
Output is correct |
30 |
Correct |
4 ms |
6092 KB |
Output is correct |
31 |
Correct |
4 ms |
6092 KB |
Output is correct |
32 |
Correct |
5 ms |
6220 KB |
Output is correct |
33 |
Correct |
4 ms |
6092 KB |
Output is correct |
34 |
Correct |
4 ms |
6092 KB |
Output is correct |
35 |
Correct |
10 ms |
7244 KB |
Output is correct |
36 |
Correct |
4 ms |
6220 KB |
Output is correct |
37 |
Correct |
12 ms |
7500 KB |
Output is correct |
38 |
Correct |
10 ms |
8396 KB |
Output is correct |
39 |
Correct |
95 ms |
17788 KB |
Output is correct |
40 |
Correct |
83 ms |
17776 KB |
Output is correct |
41 |
Correct |
40 ms |
17848 KB |
Output is correct |
42 |
Correct |
85 ms |
17768 KB |
Output is correct |
43 |
Correct |
108 ms |
22200 KB |
Output is correct |
44 |
Correct |
51 ms |
17732 KB |
Output is correct |