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!!!
Explanation / Answer
#include 02 #include 03 #include "stack.h" 04 05 #define size 3 06 07 void main() 08 { 09 int top,element; 10 int stack[size]; 11 12 // initialize stack 13 init(&top); 14 15 // push elements into stack 16 while(!full(&top,size)){ 17 element = rand(); 18 printf("push element %d into stack ",element); 19 push(stack,&top,element); 20 //press enter to push more 21 getchar(); 22 23 } 24 printf("stack is full "); 25 26 // pop elements from stack 27 while(!empty(&top)){ 28 element = pop(stack,&top); 29 printf("pop element %d from stack ",element); 30 //press enter to pop more 31 getchar(); 32 } 33 printf("stack is empty "); 34 35 getchar(); 36 }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.