Question

In: Computer Science

languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...

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,

Solutions

Expert Solution

#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;
}


Related Solutions

write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
using java write a program As the title described, you should only use two stacks to...
using java write a program As the title described, you should only use two stacks to implement a queue's actions. DO NOT use any other data structure and push, pop and top should be O(1) by AVERAGE. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the value of first element example push(1) pop() // return 1 push(2) push(3) top() // return 2...
Write a hangman program in c++.The game is played as follows: 1. The program selects a...
Write a hangman program in c++.The game is played as follows: 1. The program selects a random word for the user to guess (Read words for the user to guess into an array of strings from a file. The words are: revolutionary, meaning, recovery, compartment, trainer, pursuit, harm, platform, error, confusion) 2. User guesses one letter at a time until either they guess the word, or they run out of guesses. 3. Select a random word from the array of...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets. Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations. The program understands the following arguments: -i insertion sort -s selection sort (default) -q quicksort -a (already) sorted dataset -v reverse-sorted dataset -r random dataset (default) -n no sorting x generate...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
Write a C program that creates a database of plant height samples. In this scenario a...
Write a C program that creates a database of plant height samples. In this scenario a researcher enters a name of a group/type of plants, and heights for each plant in the group/type Use input will be stored in a database file, which should be a file called database.csv and should be in the following formatPlant1, number_of_samples, sample1, sample2, sample3 ...Plant2, number_of_samples, sample1, sample2, sample3 ...Plant3, number_of_samples, sample1, sample2, sample3 ...Lines starts with the name of a plant type, then...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT