#include "BreadthFirstPathfinder.h" #include "Museum.h" using namespace std; void BreadthFirstPathfinder::find_between(const XY & start, const XY & end) { this->clear(); vector trails = { { start } }; this->end = end; int steps = -1; while (trails.size() > 0) { steps++; trails = this->find_step(trails); } printf("BFS: %s (%d steps)\n", this->is_solved() ? "no solution found" : "solution found", steps); } vector BreadthFirstPathfinder::find_step(const vector & to_visit) { vector next_step = {}; PathfindingContext & ctx = this->museum.pathfinding; for (Path trail : to_visit) { const XY & here = trail.front(); if (here == this->end) { this->set_solved(trail); return {}; } vector offsets = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, }; for (const XY & offset : offsets) { const XY neighbor = here + offset; if (ctx.empty_point(neighbor)) continue; if (this->is_visited(neighbor)) continue; this->set_visited(neighbor); Path trail_copy = trail; trail_copy.push_front(neighbor); next_step.push_back(trail_copy); } } return next_step; }