#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) {
if(arr[posy*(maxx+2)+posx+1] != 'X') {
int t = res[posy*(maxx+2)+posx+1];
char t2 = arr[posy*(maxx+2)+posx+1];
if(t2 == 'W' && res[posy*(maxx+2)+posx] < t) {
res[posy*(maxx+2)+posx+1] = res[posy*(maxx+2)+posx];
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'N' && res[posy*(maxx+2)+posx]+3 < t) {
res[posy*(maxx+2)+posx+1] = res[posy*(maxx+2)+posx]+3;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'E' && res[posy*(maxx+2)+posx]+2 < t) {
res[posy*(maxx+2)+posx+1] = res[posy*(maxx+2)+posx]+2;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'S' && res[posy*(maxx+2)+posx]+1 < t) {
res[posy*(maxx+2)+posx+1] = res[posy*(maxx+2)+posx]+1;
f(arr, res, posy, posx+1, maxx);
}
}
if(arr[posy*(maxx+2)+posx-1] != 'X') {
int t = res[posy*(maxx+2)+posx-1];
char t2 = arr[posy*(maxx+2)+posx-1];
if(t2 == 'E' && res[posy*(maxx+2)+posx] < t) {
res[posy*(maxx+2)+posx-1] = res[posy*(maxx+2)+posx];
} else if(t2 == 'S' && res[posy*(maxx+2)+posx]+3 < t) {
res[posy*(maxx+2)+posx-1] = res[posy*(maxx+2)+posx]+3;
} else if(t2 == 'W' && res[posy*(maxx+2)+posx]+2 < t) {
res[posy*(maxx+2)+posx-1] = res[posy*(maxx+2)+posx]+2;
} else if(t2 == 'N' && res[posy*(maxx+2)+posx]+1 < t) {
res[posy*(maxx+2)+posx-1] = res[posy*(maxx+2)+posx]+1;
}
f(arr, res, posy, posx-1, maxx);
}
if(arr[(posy+1)*(maxx+2)+posx] != 'X') {
int t = res[(posy+1)*(maxx+2)+posx];
char t2 = arr[(posy+1)*(maxx+2)+posx];
if(t2== 'N' && res[posy*(maxx+2)+posx] < t) {
res[(posy+1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx];
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'E' && res[posy*(maxx+2)+posx]+3 < t) {
res[(posy+1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+3;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'S' && res[posy*(maxx+2)+posx]+2 < t) {
res[(posy+1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+2;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'W' && res[posy*(maxx+2)+posx]+1 < t) {
res[(posy+1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+1;
f(arr, res, posy+1, posx, maxx);
}
}
if(arr[(posy-1)*(maxx+2)+posx] != 'X') {
int t = res[(posy-1)*(maxx+2)+posx];
char t2 = arr[(posy-1)*(maxx+2)+posx];
if(t2 == 'S' && res[posy*(maxx+2)+posx] < t) {
res[(posy-1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx];
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'W' && res[posy*(maxx+2)+posx]+3 < t) {
res[(posy-1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+3;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'N' && res[posy*(maxx+2)+posx]+2 < t) {
res[(posy-1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+2;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'E' && res[posy*(maxx+2)+posx]+1 < t) {
res[(posy-1)*(maxx+2)+posx] = res[posy*(maxx+2)+posx]+1;
f(arr, res, posy-1, posx, maxx);
}
}
}
int main()
{
int n, m;
scanf("%d %d ", &n, &m);
char *arr = calloc((n+2)*(m+2), sizeof(char));
int *res = calloc((n+2)*(m+2), 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 = 0; i < n+2; i++) {
res[0*(m+2)+i] = -1;
arr[0*(m+2)+i] = 'X';
res[i*(m+2)+0] = -1;
arr[i*(m+2)+0] = 'X';
res[((n+2)-1)*(m+2)+i] = -1;
arr[((n+2)-1)*(m+2)+i] = 'X';
res[i*(m+2)+(m+2)-1] = -1;
arr[i*(m+2)+(m+2)-1] = 'X';
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
res[i*(m+2)+j] = inf;
}
}
res[n*(m+2)+m] = 0;
f(arr, res, n, m, m);
printf("%d", res[1*(m+2)+1]<inf?res[1*(m+2)+1]:-1); //res
free(arr);
free(res);
return 0;
}
Compilation message
adventure.c: In function 'main':
adventure.c:77:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d %d ", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~
adventure.c:82:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%c", &arr[i * (m+2) + j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.c:84:21: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | if(i+1<n+1) scanf(" ");
| ^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 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 |
420 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 |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
31 ms |
348 KB |
Output is correct |
4 |
Correct |
13 ms |
492 KB |
Output is correct |
5 |
Correct |
289 ms |
348 KB |
Output is correct |
6 |
Correct |
166 ms |
556 KB |
Output is correct |
7 |
Correct |
17 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 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 |
420 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 |
416 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
31 ms |
348 KB |
Output is correct |
28 |
Correct |
13 ms |
492 KB |
Output is correct |
29 |
Correct |
289 ms |
348 KB |
Output is correct |
30 |
Correct |
166 ms |
556 KB |
Output is correct |
31 |
Correct |
17 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
448 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
8 ms |
548 KB |
Output is correct |
37 |
Execution timed out |
2094 ms |
3392 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |