Need help writing this program... Implement a game of finding a path in a maze.
ID: 3610773 • Letter: N
Question
Need help writing this program...Implement a game of finding a path in a maze. The maze isconstructed by a 10*10 matrix with value 0 or 1. 0 indicates a wallwhich you can not cross. 1 indicates a way. The upper-left of thematrix is the entrance (E) and the lower-right is the exit (X).Atany location in the maze, at most FOUR directions are allowed: up,down, left and right. You can NOT travel diagonally.
The input is a file called maze.dat which contains a matrix, forexample:
E111111000
0010001000
0110111000
0100000000
0100011110
0111010010
0101010010
0101110110
0100000100
011000011X
The output should be a file called solution.dat. Each value on thepath of your solution is replaced by a character star (*) forexample:
E**1111000
00*0001000
0**0111000
0*00000000
0*000****0
0***0*00*0
010*0*00*0
010***0**0
0100000*00
0110000**X
the two example files are attached.
We are NOT required to find the optimal solution. You need toconsider the situation that a circle path may exist in the maze.May have to use a stack?
Implement a game of finding a path in a maze. The maze isconstructed by a 10*10 matrix with value 0 or 1. 0 indicates a wallwhich you can not cross. 1 indicates a way. The upper-left of thematrix is the entrance (E) and the lower-right is the exit (X).Atany location in the maze, at most FOUR directions are allowed: up,down, left and right. You can NOT travel diagonally.
The input is a file called maze.dat which contains a matrix, forexample:
E111111000
0010001000
0110111000
0100000000
0100011110
0111010010
0101010010
0101110110
0100000100
011000011X
The output should be a file called solution.dat. Each value on thepath of your solution is replaced by a character star (*) forexample:
E**1111000
00*0001000
0**0111000
0*00000000
0*000****0
0***0*00*0
010*0*00*0
010***0**0
0100000*00
0110000**X
the two example files are attached.
We are NOT required to find the optimal solution. You need toconsider the situation that a circle path may exist in the maze.May have to use a stack?
Explanation / Answer
Hi... Am giving you the sample code. Just try to with.If you are not getting i will give you the best. #include <fstream>#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
void InitializeMaze(ifstream&InputStream);
void Maze();
void FindExit( int row, int col, bool&success);
int Maze[10][10]; bool success = false;
int main ()
{
ifstream InputStream;
cout << endl;
cout << endl;
const int SIZE = 20;
char Name[SIZE];
cout << "Enter thename of the file";
cin >>Name;
InputStream.open(Name);
if(InputStream.fail())
{
cout <<"Inputdatafile failed .....closing" << endl;
system("pause");
exit(1);
}
InitializeMaze(InputStream);
Maze();
cout << "The Maze!"<< endl;
void FindExit( int row, intcol, bool& success);
system("pause");
return EXIT_SUCCESS;
}
voidInitializeMaze(ifstream& InputStream)
{
for (int row= 0; row < 12; row++)
{
for(int col = 0; col < 12; col++)
{
Maze[row][col] = 1;
}
}
for (int row = 1; row <= 10;row++)
{
for (int col= 1; col <= 10; col++)
{
InputStream>> Maze[row][col];
}
}
}
void Maze()
{
cout << "This is the maze withwall of 1's around it." << endl;
cout << endl;
cout << endl;
for (int row = 0; row < 12;row++)
{
for(int col = 0; col < 12; col++)
{
cout<< setw(2) << Maze[row][col];
}
cout << endl;
}
}
void FindExit( int row, int col, bool&success)
{
Maze[row][col] = 3;
cout << " reverse order:"<< endl;
if(( row == 10) && (col ==10))
{
cout << "ExitRoute : " << " row = " << row << " col = "<< col << endl;
success = true; }
else if ((!success) && (Maze[row][col + 1] ==0) )
{
cout <<" >";
cout << "ExitRoute : " << " row = " << row << " col = "<< col << endl;
FindExit(row, col + 1,success);
}
else if ((!success) && (Maze[row +1][col] == 0) )
{
cout <<" >";
cout << "Exit Route : "<< " row = " << row << " col = " << col<< endl;
FindExit(row + 1, col,success);
}
else if ((!success) && (Maze[row -1][col] == 0) )
{
cout<<" >";
cout << "ExitRoute : " << " row = " << row << " col = "<< col << endl;
FindExit(row - 1, col ,success);
}
else ((!success) &&(Maze[row][col - 1] == 0) );
{
cout <<" >";
cout << "Exit Route : "<< " row = " << row << " col = " << col<< endl;
FindExit(row, col - 1,success);
}
if (success)
{
cout << "Successful Exit!!"<< endl;
cout << endl;
cout << " << <<Exiting " << " row = " << row << " and col = "<< col << endl; }
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.