From 55b10d144a6855791f3918f37ccdf36fd8109e58 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Wed, 22 Apr 2026 20:48:27 +0300 Subject: Add a path_reverse() function --- path.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'path.c') diff --git a/path.c b/path.c index d643167..19a9aaa 100644 --- a/path.c +++ b/path.c @@ -330,6 +330,29 @@ void path_free(Path path, size_t height) { free(path); } +void path_reverse(Path *path, size_t width, size_t height, Position *start, Position *end) { + Path new_path = path_new(width, height); + + Position cur = *end; + while (cur.x != start->x || cur.y != start->y) { + Position parent = (*path)[cur.y][cur.x]; + new_path[parent.y][parent.x] = cur; + cur = parent; + } + + /* Switch start and end around */ + start->x = start->x ^ end->x; + end->x = start->x ^ end->x; + start->x = start->x ^ end->x; + + start->y = start->y ^ end->y; + end->y = start->y ^ end->y; + start->y = start->y ^ end->y; + + path_free(*path, height); + *path = new_path; +} + char **visited_new(size_t width, size_t height) { char **visited = malloc(sizeof(char*) * height); if (visited == NULL) return NULL; -- cgit v1.2.3