Mostly done

This commit is contained in:
karl 2020-10-15 14:47:10 +02:00
parent b16f9a356a
commit 101b6e973a

View File

@ -30,7 +30,7 @@ Maze solve_maze(const Maze &maze) {
costs[std::vector<uint>{1, 1}] = 1; costs[std::vector<uint>{1, 1}] = 1;
for (int i = 0; i < 270; i++) { while(true) {
// Get the best node // Get the best node
MazeNode best = nodes.front(); MazeNode best = nodes.front();
std::pop_heap(nodes.begin(), nodes.end(), [](MazeNode n1, MazeNode n2){return n1.priority > n2.priority;}); 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]; int current_y = best.v[1];
std::vector<uint> here = std::vector<uint>{current_x, current_y}; std::vector<uint> here = std::vector<uint>{current_x, current_y};
result.data[current_y][current_x] = 'P'; // Are we done?
if (current_x == 39 && current_y == 39) {
std::vector<uint> 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 if (parents.find(parent) == parents.end()) break;
uint cost = costs[here] + 1; parent = parents[parent];
}
return result;
}
for (int kernel_y = -1; kernel_y <= 1; kernel_y++) { for (int kernel_y = -1; kernel_y <= 1; kernel_y++) {
for (int kernel_x = -1; kernel_x <= 1; kernel_x++) { for (int kernel_x = -1; kernel_x <= 1; kernel_x++) {
@ -58,20 +64,16 @@ Maze solve_maze(const Maze &maze) {
// Can we go here? // Can we go here?
if (maze.data[y][x] == '#') continue; if (maze.data[y][x] == '#') continue;
// Are we done?
if (x == 39 && y == 39) {
return result;
}
std::vector<uint> next = std::vector<uint>{x, y}; std::vector<uint> next = std::vector<uint>{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 this node hasn't been added yet or its costs are better, add it!
if (costs.find(next) == costs.end() || cost < costs[next]) { if (costs.find(next) == costs.end() || cost < costs[next]) {
costs[next] = cost; costs[next] = cost;
uint priority = cost + (39 - x) + (39 - y); uint priority = cost + (39 - x) + (39 - y);
std::cout << x << ", " << y << ": Adding node with priority " << priority << std::endl;
// Add this node // Add this node
nodes.push_back(MazeNode(next, priority)); nodes.push_back(MazeNode(next, priority));
std::push_heap(nodes.begin(), nodes.end(), [](MazeNode n1, MazeNode n2){return n1.priority > n2.priority;}); std::push_heap(nodes.begin(), nodes.end(), [](MazeNode n1, MazeNode n2){return n1.priority > n2.priority;});