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) {
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 (stderr)
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(" ");
| ^~~~~~~~~~
# | 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... |