This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |