Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a maze solving program in C++ that inputs from a file and outputs the maz

ID: 3769913 • Letter: I

Question

I need a maze solving program in C++ that inputs from a file and outputs the maze with the position coordinates.
The Maze Problem: The input is an n x n maze and a start position and an exit position. Your
program needs to decide whether there is path from the start to the exit. If yes, then print the path
in a sequence of position coordinates from the start to the exit.
How to Represent a Maze: You can use a 2D array of characters. The input is from a text file.
For example, the following is a 6x6 maze:
X X S X X X
X O O O O X
X X X O O X
X O X O X X
X O O O O X
X X X X E X
Here, “X” stands for a block, “O” for an open area, “S” for the start, and “E” for the exit.
How to Output a Path: Just output the coordinates of the positions. For example for the above
ex example, the path is
S=(0, 3), (1, 2), (1, 3), (2, 3), (3, 3), (4, 3), (4, 4), E=(5, 4)

Explanation / Answer

#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


struct Stack
{
int data[30]; //X should be constant
int top;
}; //Stack

void Push(int a,Stack &S)
{
S.top++;

if (S.top>=30) //Remember X should be constant
{
cout<<"The stack is full!"<<endl;
S.top--;
return;
}

else
{
S.data[S.top]=a;
}
} //Push

int Pop(Stack &S)
{
if (S.top==-1)
{
cout<<"The stack is empty!"<<endl;
return 0;
}

else
{
int Temp=S.data[S.top];
S.data[S.top]=NULL;
S.top--;
return Temp;
}
} //Pop

int main ()
{
#define false 1;
#define true 0;
string line;
Stack s1 = { NULL, -1 }, s2 = { NULL, -1 };
int x, y; //Dimensions of Matrix.
int z; //Element Location in Matrix.
int a; //Number of Entrance Points.
int b, c; //Coordinates of Entrance Points.
char ch; //Comma indication.

ifstream input ("Input.txt"); //Using relative addresses.
ofstream output ("Output.txt");

input>>line;
cout<<"Solution for "<<line<<" : ";
input>>x>>ch>>y; //Reading the Dimensions of the Matrix.
cout<<line<<" coordinates are "<<x<<ch<<y<<endl<<endl<<endl;

int **Maze = new int *[x]; //Creating Dynamic Matrix.
for (int i=0; i<x; i++)
{
Maze[i]= new int [y];
}


for (int i=0; i<x; i++) //Filling the Maze from the .txt
{
for (int j=0; j<y; j++)
{
input>>z;
Maze[i][j]=z;
}
}


input>>a; //Reading the number of entrances.
for(int i=0; i<a; i++)
{
input>>b>>ch>>c; //Reading the entrance coordinates.
if (Maze[b][c]==1 || b>7 || c>6) //Checking for the validity of entrance point.
{
cout<<"Entrance: "<<b<<ch<<c<<" is Invalid! ";
output<<"Entrance: "<<b<<ch<<c<<" is Invalid! ";// WRITE ENTRANCE IS INVALID IN OUTPUT FILE
}

}

output.close();
input.close();


for(int i=0; i<x; i++) //Deleting Maze
{
delete[] Maze[i];
}
delete[] Maze;

return 0;
}

thank you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote