Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

turn the following C++ code into code where the library is NOT included.( youll have to make functions to create a stack, such as push,

turn the following C++ code into code where the library is NOT included.( youll have to make functions to create a stack, such as push, pop, empty... etc)other than that keep everything the same. shoukd be a simple swaping with a few function names.
// CPP program to solve Rat in a maze
// problem with backtracking using stack
#include
#include
#include
using namespace std;
#define N 4
#define M 5
class node {
public:
int x, y;
int dir;
node(int i, int j)
{
x = i;
y = j;
// Initially direction
// set to 0
dir = 0;
}
};
// maze of n*m matrix
int n = N, m = M;
// Coordinates of food
int fx, fy;
bool visited[N][M];
bool isReachable(int maze[N][M])
{
// Initially starting at (0, 0).
int i = 0, j = 0;
stack s;
node temp(i, j);
s.push(temp);
while (!s.empty()) {
// Pop the top node and move to the
// left, right, top, down or retract
// back according the value of node's
// dir variable.
temp = s.top();
int d = temp.dir;
i = temp.x, j = temp.y;
// Increment the direction and
// push the node in the stack again.
temp.dir++;
s.pop();
s.push(temp);
// If we reach the Food coordinates
// return true
if (i == fx and j == fy) {
return true;
}
// Checking the Up direction.
if (d == 0) {
if (i - 1 >= 0 and maze[i - 1][j] and
visited[i - 1][j]) {
node temp1(i - 1, j);
visited[i - 1][j] = false;
s.push(temp1);
}
}
// Checking the left direction
else if (d == 1) {
if (j - 1 >= 0 and maze[i][j - 1] and
visited[i][j - 1]) {
node temp1(i, j - 1);
visited[i][j - 1] = false;
s.push(temp1);
}
}
// Checking the down direction
else if (d == 2) {
if (i + 1
visited[i + 1][j]) {
node temp1(i + 1, j);
visited[i + 1][j] = false;
s.push(temp1);
}
}
// Checking the right direction
else if (d == 3) {
if (j + 1
visited[i][j + 1]) {
node temp1(i, j + 1);
visited[i][j + 1] = false;
s.push(temp1);
}
}
// If none of the direction can take
// the rat to the Food, retract back
// to the path where the rat came from.
else {
visited[temp.x][temp.y] = true;
s.pop();
}
}
// If the stack is empty and
// no path is found return false.
return false;
}
// Driver code
int main()
{
// Initially setting the visited
// array to true (unvisited)
memset(visited, true, sizeof(visited));
// Maze matrix
int maze[N][M] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
// Food coordinates
fx = 2;
fy = 3;
if (isReachable(maze)) {
cout
}
else
cout
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
..Il Verizon 1:15 PM geeksforgeeks.org // CPP program to solve Rat in a maze // problem with backtracking using st #include #include #include using namespace std; #de fine N 4 #de fine M 5 class node public: int x, y; int dir; node (int i, int j) x = 1; // Initially direction // set to 0 dir 0; // maze of n*m matrix 1:15 PM geeksforgeeks.org lVerizon // maze of n*m matrix int n-N, m-M // Coordinates of food int fx, fy; bool visited [N] [M] bool isReachable (int maze [N] [M]) // Initially starting at (0, 0) int i = 0, j = 0; stack si node temp(i,j) s.push (temp) while s.emptyO) // Pop the top node and move // left, right, top, down or // back according the valuec // dir variable. temp s.top O int d = temp . dir; i-temp.x, j - temp.y: .11 Verizon 1:15 PM geeksforgeeks.org // back according the valuec // dir variable. temp s.top) int d = temp.dir; // Increment the direction ar // push the node in the stack temp.dir+ s.pop s.push (temp); // If we reach the Food coorc // return true return true; // Checking the Up direction. if (d0) if (i - 1 >= 0 and ma ze [i node temp1( 1,j) visited[i1] [j] f s.push (temp1); all Verizon @ * 65% 1:15 PM geeksforgeeks.org s.push (templ); // Checking the left directic if (j - 1 >= 0 and maze [i j -1); else if (d node templ (i, visited [ ][j s.push (templ); 1] -1 - // Checking the down directic else if (d 2) if (i + 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

=+What is the expected value of purchasing a Thursday ticket?

Answered: 1 week ago

Question

What is the per-capita cost?

Answered: 1 week ago

Question

Timeline for progress report

Answered: 1 week ago