In: Computer Science
languague c++
Write a program to demonstrate the use of STACKS. The scenario is as follows:
There is a MAZE.
There is a mouse inside the maze.
The mouse must find the exit from the maze.
When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over!
User can add as many cats as he/she wants (Maximum 10)
cannot use the stl library,
#include <cstring>
#include <iostream>
#include <stack>
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;
dir = 0;
}
};
int n = N, m = M;
int fx, fy;
bool visited[N][M];
bool isReachable(int maze[N][M])
{
int i = 0, j = 0;
stack<node> s;
node temp(i, j);
s.push(temp);
while (!s.empty()) {
temp = s.top();
int d = temp.dir;
i = temp.x, j = temp.y;
temp.dir++;
s.pop();
s.push(temp);
if (i == fx and j == fy) {
return
true;
}
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);
}
}
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);
}
}
else if (d == 2) {
if (i + 1 < n
and maze[i + 1][j] and
visited[i
+ 1][j]) {
node temp1(i + 1, j);
visited[i + 1][j] = false;
s.push(temp1);
}
}
else if (d == 3) {
if (j + 1 < m
and maze[i][j + 1] and
visited[i][j + 1]) {
node temp1(i, j + 1);
visited[i][j + 1] = false;
s.push(temp1);
}
}
else {
visited[temp.x][temp.y] = true;
s.pop();
}
}
return false;
}
class node2 {
public:
int x, y;
int dir;
node(int i, int j)
{
x = i;
y = j;
dir = 0;
}
};
int n = N, m = M;
int fx, fy;
bool visited[N][M];
bool CatisReachable(int maze[N][M])
{
int i = 0, j = 0;
stack<node> s;
node temp(i, j);
s.push(temp);
while (!s.empty()) {
temp = s.top();
int d = temp.dir;
i = temp.x, j = temp.y;
temp.dir++;
s.pop();
s.push(temp);
if (i == fx and j == fy) {
return
true;
}
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);
}
}
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);
}
}
else if (d == 2) {
if (i + 1 < n
and maze[i + 1][j] and
visited[i
+ 1][j]) {
node temp1(i + 1, j);
visited[i + 1][j] = false;
s.push(temp1);
}
}
else if (d == 3) {
if (j + 1 < m
and maze[i][j + 1] and
visited[i][j + 1]) {
node temp1(i, j + 1);
visited[i][j + 1] = false;
s.push(temp1);
}
}
else {
visited[temp.x][temp.y] = true;
s.pop();
}
}
return false;
}
int main()
{
memset(visited, true, sizeof(visited));
memset(visited, true, sizeof(visited));
int maze[N][M] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
//exit coordinates
fx = 2;
fy = 3;
if (isReachable(maze)) {
cout << "Mouse Wins!"
<< '\n';
}
else
cout << "No Path Found!"
<< '\n';
if (CatisReachable(maze)) {
cout << "Cat Wins!" <<
'\n';
}
else
cout << "No Path Found!"
<< '\n';
cout<<"enter the num of cats"<<'\n';
cin>>k;
cat=0;
if (k=='C' || k=='c')
{
for(int i=0;i<k;i++)
cat++;
}
return 0;
}