Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Navigating a Maze(C++ Code) MazePath.h: #include #include #ifndef MAZEPATH_H #define MAZEPATH_H class MazePath { public: const static std::string DIRECTIONS; const static std::string COMPLEMENTS; //CONSTRUCTORS MazePath();
Navigating a Maze(C++ Code)
MazePath.h:
#include #include #ifndef MAZEPATH_H #define MAZEPATH_H class MazePath { public: const static std::string DIRECTIONS; const static std::string COMPLEMENTS; //CONSTRUCTORS MazePath(); MazePath(std::string steps); //takes a step further into the maze void takeStep(char step); //returns the sequence of steps to get back to the starting point std::string directionsOut(); //overloaded output operator friend std::ostream& operatorMazePath.cxx
#include "MazePath.h" #include #include #include using namespace std; const string MazePath::DIRECTIONS = "NSEW"; const string MazePath::COMPLEMENTS = "SNWE"; MazePath::MazePath() { } MazePath::MazePath(string steps) { for (int ndx = 0; ndxMazePathTest.cpp
#include "MazePath.h" #include #include using namespace std; int main () { //Testing isValid function with valid strings coutMaze.h
#include #ifndef MAZE_H #define MAZE_H class Maze { public: static const char EMPTY = '.'; static const char BLOCKED = 'X'; static const char START = 'S'; static const char END = 'G'; // Creates a Maze with no blocked cells; start = (0, 0); goal = (SIZE, SIZE) Maze(); // Puts obstacle on the cell (row, col) void blockCell(int row, int col); // is position (row, col) clear? bool isClear(int row, int col) const; // returns the size of the Maze int size() const; // clear the maze: erase all obstacles void clearAllCells(); // sets the end cell to (col, row) void setEndCell(int row, int col); // sets the start cell to (col, row) void setStartCell(int row, int col); // find a path from S to G by backtracking std::string solveBacktracking(); // overloaded output operator friend std::ostream& operatorMaze.cxx:
#include "Maze.h"
#include using namespace std; // Creates a Maze with no blocked cells; start = (0, 0); goal = (SIZE, SIZE) Maze::Maze(){ for (int row = 0; row Navigating a Maze Finding a path Assume that we have not been given the steps that the robot took to go inside a maze and we are left with the task of getting the robot out of the maze We can generalize this problem as finding a path from a start position to a goal position in a maze. A simple solution will simply have the robot navigate through available paths in the maze until it finds its way to the goal state, backtracking when it gets stuck. A backtracking algorithm for such solution is the following 1. Mark every square in the maze as unvisited 2. Create an empty stack (of maze positions) 3. Push the start positions onto the stack, and mark the start square as visited 4. If the stack is empty, you're done and the maze is unsolvable 5. Let T be the top item on the stack. If T is equal to the finish square, you're done 6. If all squares adjacent to T (i.e. the squares up, down, right, or left from T) are 7. Otherwise, select a square S that is adjacent to T, unvisited, and unblocked and the stack contains a solution to the maze either blocked or are marked visited already, pop T off the stack and go to step 4 Mark S as visited and push its positions on the stack. Go to step 4 Navigating a Maze Finding a path Assume that we have not been given the steps that the robot took to go inside a maze and we are left with the task of getting the robot out of the maze We can generalize this problem as finding a path from a start position to a goal position in a maze. A simple solution will simply have the robot navigate through available paths in the maze until it finds its way to the goal state, backtracking when it gets stuck. A backtracking algorithm for such solution is the following 1. Mark every square in the maze as unvisited 2. Create an empty stack (of maze positions) 3. Push the start positions onto the stack, and mark the start square as visited 4. If the stack is empty, you're done and the maze is unsolvable 5. Let T be the top item on the stack. If T is equal to the finish square, you're done 6. If all squares adjacent to T (i.e. the squares up, down, right, or left from T) are 7. Otherwise, select a square S that is adjacent to T, unvisited, and unblocked and the stack contains a solution to the maze either blocked or are marked visited already, pop T off the stack and go to step 4 Mark S as visited and push its positions on the stack. Go to step 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started