From 28e7f356b7d7c30326b66674c06af339b350c05c Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sat, 23 Dec 2023 11:27:55 +0100 Subject: Added solution for 23 --- 2023/23/23a.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2023/23/23a.c (limited to '2023/23/23a.c') diff --git a/2023/23/23a.c b/2023/23/23a.c new file mode 100644 index 0000000..fb6ac7d --- /dev/null +++ b/2023/23/23a.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#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; +} -- cgit v1.3