okay i am having a problem with implementing my function Path_exist_from(const c
ID: 3638466 • Letter: O
Question
okay i am having a problem with implementing my function Path_exist_from(const coordinate& s) which turns true if there is a path from s to the exit, and mark the path to value 2 at the same time (The cells that lead to the exit will be changed from0 to 2). If there is no path the function returns false.
Please help!!!
Here is my code so far: In C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class coordiate {
public:
int x,y;
coordiate(int a,int b) {
x = a; y = b;
}
coordiate() {
}
};
class maze {
int **m;
coordiate entrance,exit;
int size;
public:
maze(int n) { // create a maze of size n*n
int i,j;
size = n;
m = new int* [n];
for (i=0;i<n;i++) {
m[i] = new int [n];
}
// randomly assign values to maze. 0 means empty, 1 means wall
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
if (rand() % 3 == 0) m[i][j] = 1; // 25% change wall
else m[i][j] = 0; // 75% chance empty space
}
}
// set entrance and exit
entrance.x = entrance.y = 0;
exit.x = exit.y = size-1;
// entrance and exit always empty
m[entrance.x][entrance.y] = 0;
m[exit.x][exit.y] = 0;
}
~maze() {
int i;
for (i=0;i<size;i++) delete [] m[i];
delete m;
}
void print() {
int i,j;
for (i=0;i<size;i++) {
for (j=0;j<size;j++) {
cout << m[i][j];
}
cout << endl;
}
}
// returns true if path from s to exit exists. returns false otherwise
// Mark the path to value 2
bool path_exist_from(const coordiate& s) {
//<------ cant get my coding right here
}
bool path_exist() {
return path_exist_from(entrance);
}
};
int main()
{
int n;
srand(time(NULL)); // random number seed
cout << "Enter the size of maze:";
cin >> n;
maze mymaze(n);
mymaze.print();
if (mymaze.path_exist()) {
cout << "Path found!" << endl;
mymaze.print();
} else {
cout << "Path not found..." << endl;
}
return 0;
}
Any little bit of correct running code will be helpful...thanks for your time!!!