From 101b6e973af25c12db70b695749af73b856381e4 Mon Sep 17 00:00:00 2001 From: karl Date: Thu, 15 Oct 2020 14:47:10 +0200 Subject: [PATCH] Mostly done --- main.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index ed962e6..766c6e8 100644 --- a/main.cpp +++ b/main.cpp @@ -30,7 +30,7 @@ Maze solve_maze(const Maze &maze) { costs[std::vector{1, 1}] = 1; - for (int i = 0; i < 270; i++) { + while(true) { // Get the best node MazeNode best = nodes.front(); std::pop_heap(nodes.begin(), nodes.end(), [](MazeNode n1, MazeNode n2){return n1.priority > n2.priority;}); @@ -40,12 +40,18 @@ Maze solve_maze(const Maze &maze) { int current_y = best.v[1]; std::vector here = std::vector{current_x, current_y}; - result.data[current_y][current_x] = 'P'; + // Are we done? + if (current_x == 39 && current_y == 39) { + std::vector parent = here; - std::cout << i << ": " << current_x << ", " << current_y << " with " << " and priority " << best.priority << std::endl; + while (true) { + result.data[parent[1]][parent[0]] = 'o'; - // The cost is the travelled distance to the next node - uint cost = costs[here] + 1; + if (parents.find(parent) == parents.end()) break; + parent = parents[parent]; + } + return result; + } for (int kernel_y = -1; kernel_y <= 1; kernel_y++) { for (int kernel_x = -1; kernel_x <= 1; kernel_x++) { @@ -58,20 +64,16 @@ Maze solve_maze(const Maze &maze) { // Can we go here? if (maze.data[y][x] == '#') continue; - // Are we done? - if (x == 39 && y == 39) { - return result; - } - std::vector next = std::vector{x, y}; + // The cost is the travelled distance to the next node + uint cost = costs[here] + abs(kernel_y) + abs(kernel_x); + // If this node hasn't been added yet or its costs are better, add it! if (costs.find(next) == costs.end() || cost < costs[next]) { costs[next] = cost; uint priority = cost + (39 - x) + (39 - y); - std::cout << x << ", " << y << ": Adding node with priority " << priority << std::endl; - // Add this node nodes.push_back(MazeNode(next, priority)); std::push_heap(nodes.begin(), nodes.end(), [](MazeNode n1, MazeNode n2){return n1.priority > n2.priority;});