vector<Point> Solver::BFSolve(Maze::Ptr const& maze) { queue<vector<Point> > paths; Point current = maze->GetStart(); vector<Point> path; path.push_back(current); paths.push(path); vector<vector<bool> > marked(maze->Columns(), vector<bool>(maze->Rows(), false)); while (!paths.empty()) { path = paths.front(); paths.pop(); current = path.back(); if (maze->GetFinish() == current) return path; if (maze->RightOpen(current) && !marked[current.Right().x()][current.Right().y()]) { path.push_back(current.Right()); paths.push(path); path.pop_back(); marked[current.Right().x()][current.Right().y()] = true; } if (maze->DownOpen(current) && !marked[current.Down().x()][current.Down().y()]) { path.push_back(current.Down()); paths.push(path); path.pop_back(); marked[current.Down().x()][current.Down().y()] = true; } if (maze->LeftOpen(current) && !marked[current.Left().x()][current.Left().y()]) { path.push_back(current.Left()); paths.push(path); path.pop_back(); marked[current.Left().x()][current.Left().y()] = true; } if (maze->UpOpen(current) && !marked[current.Up().x()][current.Up().y()]) { path.push_back(current.Up()); paths.push(path); path.pop_back(); marked[current.Up().x()][current.Up().y()] = true; } } return path; }
vector<Point> Solver::DFSolve(Maze::Ptr maze) { vector<Point> path; Point current = maze->GetStart(); vector<vector<bool> > marked(maze->Columns(), vector<bool>(maze->Rows(), false)); path.push_back(current); marked[current.x()][current.y()] = true; while (!path.empty()) { current = path.back(); if (maze->GetFinish() == current) return path; if (maze->RightOpen(current) && !marked[current.Right().x()][current.Right().y()]) { // Add? path.push_back(current.Right()); marked[current.Right().x()][current.Right().y()] = true; } else if (maze->DownOpen(current) && !marked[current.Down().x()][current.Down().y()]) { path.push_back(current.Down()); marked[current.Down().x()][current.Down().y()] = true; } else if (maze->LeftOpen(current) && !marked[current.Left().x()][current.Left().y()]) { path.push_back(current.Left()); marked[current.Left().x()][current.Left().y()] = true; } else if (maze->UpOpen(current) && !marked[current.Up().x()][current.Up().y()]) { path.push_back(current.Up()); marked[current.Up().x()][current.Up().y()] = true; } else { path.pop_back(); } } return path; }
Cell::Direction PrimsMaze::GetOppositeParentsDirection(Point& current) { Cell cell = Get(current); if (cell.GetParent() == &Get(current.Right())) return Cell::Direction::LEFT; else if (cell.GetParent() == &Get(current.Down())) return Cell::Direction::UP; else if (cell.GetParent() == &Get(current.Left())) return Cell::Direction::RIGHT; else return Cell::Direction::DOWN; }