23a.c (874B)
1 #include <stdbool.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #define N 200 6 7 bool v[N][N]; 8 char map[N][N]; 9 int n, s, t, sol; 10 11 int max(int x, int y) { return x > y ? x : y; } 12 13 int longpath(int i, int j) { 14 if (i == n-1 && j == t) return 0; 15 if (v[i][j]) return -1; 16 17 char c = map[i][j]; 18 int ret = -1; 19 v[i][j] = true; 20 if (c == '.' || c == '>') ret = max(ret, longpath(i, j+1)); 21 if (c == '.' || c == 'v') ret = max(ret, longpath(i+1, j)); 22 if (c == '.' || c == '<') ret = max(ret, longpath(i, j-1)); 23 if (c == '.' || c == '^') ret = max(ret, longpath(i-1, j)); 24 v[i][j] = false; 25 26 return ret == -1 ? ret : ret + 1; 27 } 28 29 int main() { 30 for (n = 1; fgets(map[n], N, stdin) != NULL; n++) ; 31 memset(map[0], '#', n); 32 for (int i = 0; i < n; i++) if (map[1][i] == '.') s = i; 33 for (int i = 0; i < n; i++) if (map[n-1][i] == '.') t = i; 34 35 printf("%d\n", longpath(1, s)); 36 return 0; 37 }