#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;
static int t3;
t3 = res[posy*(maxx+2)+posx];
if(arr[posy*(maxx+2)+posx+1] != 'X') {
t = res[posy*(maxx+2)+posx+1];
t2 = arr[posy*(maxx+2)+posx+1];
if(t2 == 'W' && t3 < t) {
res[posy*(maxx+2)+posx+1] = t3;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'N' && res[posy*(maxx+2)+posx]+3 < t) {
res[posy*(maxx+2)+posx+1] = t3+3;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'E' && t3 < t) {
res[posy*(maxx+2)+posx+1] = t3+2;
f(arr, res, posy, posx+1, maxx);
} else if(t2 == 'S' && t3+1 < t) {
res[posy*(maxx+2)+posx+1] = t3+1;
f(arr, res, posy, posx+1, maxx);
}
}
if(arr[posy*(maxx+2)+posx-1] != 'X') {
t = res[posy*(maxx+2)+posx-1];
t2 = arr[posy*(maxx+2)+posx-1];
if(t2 == 'E' && t3 < t) {
res[posy*(maxx+2)+posx-1] = t3;
} else if(t2 == 'S' && t3+3 < t) {
res[posy*(maxx+2)+posx-1] = t3+3;
} else if(t2 == 'W' && t3+2 < t) {
res[posy*(maxx+2)+posx-1] = t3+2;
} else if(t2 == 'N' && t3+1 < t) {
res[posy*(maxx+2)+posx-1] = t3+1;
}
f(arr, res, posy, posx-1, maxx);
}
if(arr[(posy+1)*(maxx+2)+posx] != 'X') {
t = res[(posy+1)*(maxx+2)+posx];
t2 = arr[(posy+1)*(maxx+2)+posx];
if(t2== 'N' && t3 < t) {
res[(posy+1)*(maxx+2)+posx] = t3;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'E' && t3+3 < t) {
res[(posy+1)*(maxx+2)+posx] = t3+3;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'S' && t3+2 < t) {
res[(posy+1)*(maxx+2)+posx] = t3+2;
f(arr, res, posy+1, posx, maxx);
} else if(t2 == 'W' && t3+1 < t) {
res[(posy+1)*(maxx+2)+posx] = t3+1;
f(arr, res, posy+1, posx, maxx);
}
}
if(arr[(posy-1)*(maxx+2)+posx] != 'X') {
t = res[(posy-1)*(maxx+2)+posx];
t2 = arr[(posy-1)*(maxx+2)+posx];
if(t2 == 'S' && t3 < t) {
res[(posy-1)*(maxx+2)+posx] = t3;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'W' && t3 < t) {
res[(posy-1)*(maxx+2)+posx] = t3+3;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'N' && t3+2 < t) {
res[(posy-1)*(maxx+2)+posx] = t3+2;
f(arr, res, posy-1, posx, maxx);
} else if(t2 == 'E' && t3+1 < t) {
res[(posy-1)*(maxx+2)+posx] = 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 = 0; i < n+2; 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[1*(m+2)+1];
printf("%d", n<inf?n:-1); //res
free(arr);
free(res);
return 0;
}
Compilation message
adventure.c: In function 'main':
adventure.c:81:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d %d ", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~
adventure.c:86:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%c", &arr[i*(m+2)+j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.c:88:21: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | if(i+1<n+1) scanf(" ");
| ^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
416 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |