Problem 2: Maze Suppose we need to determine whether or not a particular maze ca
ID: 3731566 • Letter: P
Question
Problem 2: Maze Suppose we need to determine whether or not a particular maze can be solved. We will think of the maze as composed of tall thick green hedges surrounding numerous paths. There will be a single entrance to the maze and a place where we want to get: the goal. On the computer, a maze will be a two-dimensional array of characters such: u GH H H b. HHHH HHHHHHHnHHHnoHHHHHIHIHHIHIHIHTH Where each 'H represents the hedge, the 'E' represent the entrance to the maze, the 'G represents the goal position and the blanks represent the paths between hedges. We will assume that the maze is stored in file called maze. Write an algorithm and the corresponding code to: read in the maze 2. mark all location tried with a dot ". " and print the new maze to a file call maze.out. 3. Print message to say whether there is a path to the goal or not. l. mar al location tried with a dot and he new mazetoa file alExplanation / Answer
/* C/C++ program to solve Rat in a Maze problem using
backtracking */
#include<stdio.h>
#include <iostream>
using namespace std;
// Maze size
#define N 100 // maximum size of maze
int n;
bool solveMazeUtil(char maze[N][N], int x, int y, int sol[N][N], string direction);
/* A utility function to print solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf(" %d ", sol[i][j]);
printf(" ");
}
}
/* A utility function to check if x,y is valid index for N*N maze */
bool isSafe(char maze[N][N], int x, int y)
{
// if (x,y outside maze) return false
if(x >= 0 && x < n && y >= 0 && y < n && (maze[x][y] == 'B' || maze[x][y] == 'E' ))
return true;
return false;
}
/* This function solves the Maze problem using Backtracking. It mainly
uses solveMazeUtil() to solve the problem. It returns false if no
path is possible, otherwise return true and prints the path in the
form of 1s. Please note that there may be more than one solutions,
this function prints one of the feasible solutions.*/
bool solveMaze(char maze[N][N])
{
int sol[N][N];
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
sol[i][j] = 0;
}
int start_i ,start_j;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if (maze[i][j] == 'E')
{
start_i = i;
start_j = j;
}
}
}
if(solveMazeUtil(maze, start_i, start_j , sol,"down") == false)
{
printf("Solution doesn't exist");
return false;
}
printSolution(sol);
return true;
}
/* A recursive utility function to solve Maze problem */
bool solveMazeUtil(char maze[N][N], int x, int y, int sol[N][N], string direction)
{
// if (x,y is goal) return true
if(maze[x][y] == 'G')
{
sol[x][y] = 1;
return true;
}
// Check if maze[x][y] is valid
if(isSafe(maze, x, y) == true)
{
cout << "x is " << x << " y is "<< y << endl;
// mark x,y as part of solution path
sol[x][y] = 1;
/* Move forward in x direction */
if (direction!="up" && solveMazeUtil(maze, x+1, y, sol, "down") == true)
return true;
/* If moving in x direction doesn't give solution then
Move down in y direction */
if (direction!="left" && solveMazeUtil(maze, x, y+1, sol,"right") == true)
return true;
if (direction!="down" && solveMazeUtil(maze, x-1, y, sol,"up") == true)
return true;
if (direction!="right" && solveMazeUtil(maze, x, y-1, sol,"left") == true)
return true;
/* If none of the above movements work then BACKTRACK:
unmark x,y as part of solution path */
sol[x][y] = 0;
return false;
}
return false;
}
// driver program to test above function
int main()
{
std::cin >> n;
char maze[N][N];
cout << "input entered"<< endl;
for(int i =0;i<n;i++)
{
cout << "enter line"<<endl;
string input;
cin >> input;
for (int j=0;j<n;j++)
{
maze[i][j] = input[j];
}
}
cout << "mazr iod "<< endl;
for(int i =0;i<n;i++)
{
for (int j=0;j<n;j++)
{
cout << maze[i][j] << " ";
}
cout << endl;
}
solveMaze(maze);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.