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