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.
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...
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 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...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
To write a C++ program for following scenario and display requirements: Scenario-based Problem: AIG Insurance wants...
To write a C++ program for following scenario and display requirements: Scenario-based Problem: AIG Insurance wants to create an insurance management system for their clients. The insurance management system will compute the required payments from the clients. The commission of the agent shall also be computed which depends on the amount of insurance type. Insurance Type Amount of Insurance type Agent Commission Life 2500 12.5% of amount Health 1500 10.5% of amount Other inputs 0 0 Computation of monthly payments...
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules...
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: 1. Initialize pfx to an empty expression and also initialize the stack. 2. Get the next symbol, sym, from infx. a. If sym is an operand, append sym to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT