#include <stdio.h>
#include <stdlib.h>
const int inf = 500*500*3+2;
void f(char *arr, int *res, int posy, int posx, int maxx) {
static int t;
static char t2;
int t3 = res[(posy-1)*maxx+posx-1];
if(arr[posy*(maxx+2)+posx+1] != 'X') {
t = res[(posy-1)*maxx+posx];
t2 = arr[posy*(maxx+2)+posx+1];
if(t2 == 'W' && t3 < t) {
res[(posy-1)*maxx+posx] = t3;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'N' && t3+3 < t) {
res[(posy-1)*maxx+posx] = t3+3;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'E' && t3+2 < t) {
res[(posy-1)*maxx+posx] = t3+2;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'S' && t3+1 < t) {
res[(posy-1)*maxx+posx] = t3+1;
f(arr, res, posy, posx+1, maxx);
}
}
if(arr[posy*(maxx+2)+posx-1] != 'X') {
t = res[(posy-1)*maxx+posx-2];
t2 = arr[posy*(maxx+2)+posx-1];
if(t2 == 'E' && t3 < t) {
res[(posy-1)*maxx+posx-2] = t3;
f(arr, res, posy, posx-1, maxx);
} else if(t2 == 'S' && t3+3 < t) {
res[(posy-1)*maxx+posx-2] = t3+3;
f(arr, res, posy, posx-1, maxx);
} else if(t2 == 'W' && t3+2 < t) {
res[(posy-1)*maxx+posx-2] = t3+2;
f(arr, res, posy, posx-1, maxx);
} else if(t2 == 'N' && t3+1 < t) {
res[(posy-1)*maxx+posx-2] = t3+1;
f(arr, res, posy, posx-1, maxx);
}
}
if(arr[(posy+1)*(maxx+2)+posx] != 'X') {
t = res[posy*maxx+posx-1];
t2 = arr[(posy+1)*(maxx+2)+posx];
if(t2== 'N' && t3 < t) {
res[posy*maxx+posx-1] = t3;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'E' && t3+3 < t) {
res[posy*maxx+posx-1] = t3+3;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'S' && t3+2 < t) {
res[posy*maxx+posx-1] = t3+2;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'W' && t3+1 < t) {
res[posy*maxx+posx-1] = t3+1;
f(arr, res, posy+1, posx, maxx);
}
}
if(arr[(posy-1)*(maxx+2)+posx] != 'X') {
t = res[(posy-2)*maxx+posx-1];
t2 = arr[(posy-1)*(maxx+2)+posx];
if(t2 == 'S' && t3 < t) {
res[(posy-2)*maxx+posx-1] = t3;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'W' && t3+3 < t) {
res[(posy-2)*maxx+posx-1] = t3+3;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'N' && t3+2 < t) {
res[(posy-2)*maxx+posx-1] = t3+2;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'E' && t3+1 < t) {
res[(posy-2)*maxx+posx-1] = t3+1;
f(arr, res, posy-1, posx, maxx);
}
}
}
int main()
{
int n, m;
scanf("%d %d ", &n, &m);
char *arr = malloc((n+2)*(m+2)*sizeof(char));
int *res = malloc(n*m*sizeof(int));
for(int i = 1; i < n+1; i++) {
for(int j = 1; j < m+1; j++) {
scanf("%c", &arr[i * (m+2) + j]);
}
if(i+1<n+1) scanf(" ");
}
for(int i = 1; i < n+1; i++) {
arr[0*(m+2)+i] = 'X';
arr[i*(m+2)+0] = 'X';
arr[((n+2)-1)*(m+2)+i] = 'X';
arr[i*(m+2)+(m+2)-1] = 'X';
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
res[i*m+j] = inf;
}
}
res[(n-1)*m+m-1] = 0;
f(arr, res, n, m, m);
n = res[0*m+0];
printf("%d", n<inf?n:-1); //res
return 0;
}
Compilation message
adventure.c: In function 'main':
adventure.c:83:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d %d ", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~
adventure.c:88:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%c", &arr[i * (m+2) + j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.c:90:21: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | if(i+1<n+1) scanf(" ");
| ^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
420 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
420 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
13 ms |
548 KB |
Output is correct |
37 |
Execution timed out |
2087 ms |
1884 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |