Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <iostream>
- # include <vector>
- # include <queue>
- # include "maze.hpp"
- struct Room {
- public:
- Room (int const RoomX, int const RoomY, RoomType const Type)
- : _RoomX (RoomX)
- , _RoomY (RoomY)
- , _Type (Type)
- {}
- int const _RoomX;
- int const _RoomY;
- RoomType const _Type;
- };
- std:: vector <Room> RoomsAround (Maze & M, int const RoomX, int const RoomY)
- {
- std:: vector <Room> RList;
- for (int X = RoomX + 1; X >= RoomX - 1; X --) {
- for (int Y = RoomY + 1; Y >= RoomY - 1; Y --) {
- RList.emplace_back (Room (X, Y,
- M.GetRoomType (X, Y)));
- }
- }
- return RList;
- }
- bool FindPath (Maze & M, std:: queue <Room> & Graph) {
- if (Graph.empty ()) {
- std:: cout << "Failed! ";
- return false;
- }
- if (Graph.front ()._Type == RoomType:: Treasure)
- return true;
- else {
- auto OldNode = Graph.front ();
- M.MarkPath (OldNode._RoomX, OldNode._RoomY);
- for (const auto & Child: RoomsAround (M, OldNode._RoomX,
- OldNode._RoomY)) {
- if (Child._Type == RoomType:: EmptyRoom ||
- Child._Type == RoomType:: Treasure) {
- Graph.push (Child);
- }
- }
- Graph.pop ();
- }
- return FindPath (M, Graph);
- }
- bool FindPath (Maze & M)
- {
- std:: queue <Room> Graph;
- int StartX, StartY;
- M.GetStartCoordinates (StartX, StartY);
- Room StartingRoom (StartX, StartY, M.GetRoomType (StartX, StartY));
- Graph.push (StartingRoom);
- return FindPath (M, Graph);
- }
- int main (int, char **)
- {
- Maze M;
- if (! FindPath (M)) {
- std:: cout << "No path found in the graph.\nTraversed nodes: \n";
- } else {
- std:: cout << "Path found.\nTraversed nodes: \n";
- }
- std:: cout << M;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement