23b.c (2090B)
1 #include <stdbool.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #define N 150 6 #define V 1000 7 8 typedef struct { int i, j; } node_t; 9 typedef struct { int u, w; } edge_t; 10 int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; 11 12 bool v[N][N]; 13 char map[N][N]; 14 int n, s, t, nv, ne[V]; 15 node_t node[V]; 16 edge_t g[V][4]; 17 18 int max(int x, int y) { return x > y ? x : y; } 19 20 bool isjunc(int i, int j) { 21 int nn = 0; 22 for (int k = 0; k < 4; k++) 23 nn += map[i+d[k][0]][j+d[k][1]] == '#'; 24 return nn != 2; 25 } 26 27 void advance(int io, int jo, int *i, int *j) { 28 for (int k = 0; k < 4; k++) { 29 int ni = *i + d[k][0], nj = *j + d[k][1]; 30 if (!v[ni][nj] && (ni != io || nj != jo) && 31 map[ni][nj] != '#') { 32 *i = ni; 33 *j = nj; 34 if (!isjunc(*i, *j)) v[*i][*j] = true; 35 return; 36 } 37 } 38 } 39 40 int findorcreate(int i, int j) { 41 for (int u = 0; u < nv; u++) 42 if (node[u].i == i && node[u].j == j) 43 return u; 44 node[nv] = (node_t) {.i = i, .j = j}; 45 return nv++; 46 } 47 48 edge_t findedge(int io, int jo, int i, int j) { 49 int w; 50 v[i][j] = true; 51 for (w = 1; !isjunc(i, j); w++) advance(io, jo, &i, &j); 52 return (edge_t) {.u = findorcreate(i, j), .w = w}; 53 } 54 55 void makegraph(int u) { 56 int i = node[u].i, j = node[u].j; 57 for (int k = 0; k < 4; k++) { 58 int ni = i + d[k][0], nj = j + d[k][1]; 59 if (!v[ni][nj] && map[ni][nj] != '#') { 60 edge_t e = findedge(i, j, ni, nj); 61 g[u][ne[u]++] = (edge_t) {.u = e.u, .w = e.w}; 62 g[e.u][ne[e.u]++] = (edge_t) {.u = u, .w = e.w}; 63 } 64 } 65 } 66 67 int longpath(int u) { 68 int i = node[u].i, j = node[u].j, k, ret, p; 69 70 if (i == n-1 && j == t) return 0; 71 if (v[i][j]) return -1; 72 73 v[i][j] = true; 74 for (ret = -1, k = 0; k < ne[u]; k++) 75 if ((p = longpath(g[u][k].u)) != -1) 76 ret = max(ret, g[u][k].w + p); 77 v[i][j] = false; 78 79 return ret; 80 } 81 82 int main() { 83 for (n = 1; fgets(map[n], N, stdin) != NULL; n++) ; 84 memset(map[0], '#', n); 85 memset(map[n], '#', n); 86 for (int i = 0; i < n; i++) if (map[1][i] == '.') s = i; 87 for (int i = 0; i < n; i++) if (map[n-1][i] == '.') t = i; 88 89 findorcreate(1, s); 90 for (int u = 0; u < nv; u++) 91 makegraph(u); 92 93 printf("%d\n", longpath(0)); 94 95 return 0; 96 }