Question

In: Computer Science

You are required to become familiar with the workings of the Rat in a Maze program...

You are required to become familiar with the workings of the Rat in a Maze program and make certain adjustments to the program. To become familiar with the program I suggest that you run it several times, using a different maze and maybe a different food location. Also trace by hand to ensure you understand how each step works. The adjustments to be made are:

1. Allow the RAT to move in a diagonal direction, so now there are a maximum of eight possible moves

2. Write a function to print the path from the start state (initial location of the RAT), to the goal state (location of the food)

Your program should prompt the user for the coordinates of the initial state and of the goal state.

Solutions

Expert Solution

The program is 0 index based. I have used BFS to find the shortest path and printing it

All the eight directions are represted using pairs like (1,0) -> mean can move left

And at the end there is one demo test case to check the program

Program

#include"iostream"
#include"vector"
#include"queue"
#include"stack"
using namespace std;

struct node
{
   int x;
   int y;
   int pre;
};
int m, n;
int head = 0; int tail = 1;
node start, EN;

// Pair showing it can move in 8 directions
int dis[8][2] = { { 1, 1},{ -1, 0 },{ 1, 0 },{1, -1},{-1, 1},{ 0, -1 },{ 0, 1 },{-1, -1} };


// Print and respective output of final results
void print_path(int tail, node* que)
{
    if (que[tail].pre != -1)
        print_path(que[tail].pre, que);
   cout << "{" << que[tail].x << "," << que[tail].y << "}" << "\n";
   return;
}

// Shortest path using BFS
int BFS(vector<vector<int>> maze, node* que)
{
   node point;
   point = start;
   que[head] = point;
   vector<vector<int>> flag(m, vector<int>(n, 0));
   flag[point.x][point.y] = 1;
   while (head<tail)
   {
       for (int i = 0; i<8; i++)
       {
           int tx = que[head].x + dis[i][0];
           int ty = que[head].y + dis[i][1];

           if (tx<m && tx >= 0 && ty<n && ty >= 0 && maze[tx][ty] == 0)
           if (flag[tx][ty] == 0)
           {
               que[tail].x = tx;
               que[tail].y = ty;
               que[tail].pre = head;
               flag[tx][ty] = 1;
               if (tx == EN.x && ty == EN.y)
               {
                   //head is the first step to the end point.
                   return tail;
               }
               tail++;
           }
       }
       head++;
   }
   return 0;
}

int main()
{
        cin>>m>>n;   // Size of maze
        node *que = new node[n*m];
        head = 0; tail = 1;
      
        // Taking start and end as the input
        int sx,sy,ex,ey;
        cin>>sx>>sy>>ex>>ey;
      
        // Assigning the values
       start.x = sx; start.y = sy; start.pre = -1;
       EN.x = ex; EN.y = ey; EN.pre = -1;
      
        // Taking maze as the input where 1 means wall and 0 means empty path
       vector<vector<int>> maze(m, vector<int>(n, 0));
       for (int i = 0; i<m; i++){
           for (int j = 0; j<n; j++)
           {
               cin >> maze[i][j];
           }
       }
      
        // Finding the shortest path
       int tail = BFS(maze, que);
      
        // Printing the path
        print_path(tail, que);
       delete []que;
      
   return 0;
}

Example

5 5    // size
0 0   // start
3 4   // End


0 0 0 0 0  
1 1 0 0 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0

// Path
{0,0}
{0,1}
{1,2}
{1,3}
{2,4}
{3,4}


Related Solutions

In a maze running study, a rat is run in a T maze and the result...
In a maze running study, a rat is run in a T maze and the result of each run recorded. A reward in the form of food is always placed at the right exit. If learning is taking place, the rat will choose the right exit more often than the left. If no learning is taking place, the rat should randomly choose either exit. Suppose that the rat is given n = 100 runs in the maze and that he...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
Become familiar with the general basics of the regulatory rules applying to insider trading. You are...
Become familiar with the general basics of the regulatory rules applying to insider trading. You are not expected to become an expert in this topic. Apply this rule to the facts of this very brief case: Someone you know has knowledge of an impending merger between two companies. The combination of the two firms will certainly change the market dynamics of the industry and owners of stock in either company will greatly benefit, once the news of the merger is...
Instructions: Answer ALL questions in the examination booklet. You are required to show your workings unless...
Instructions: Answer ALL questions in the examination booklet. You are required to show your workings unless stated otherwise. QUESTION 1 What is the lowest z-value for the lowest 5% of observations on the standard normal distribution?    If we know that the length of time it takes a university student to find a parking space in the university car park follows a normal distribution with a mean of 3.5 minutes and a standard deviation of 1 minute, find the Interquartile...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar with and practice the measurement of Net Present Value (NPV), payback, and Weighted Average Cost of Capital (WACC) using Microsoft® Excel®. Assignment Steps Resources: Microsoft® Excel®, Capital Budgeting Decision Models Template Calculate the following problems using Microsoft® Excel®: Calculate the NPV for each project and determine which project should be accepted. Project A Project B Project C Project D Inital Outlay (105,000.000) (99,000.00) (110,000.00)...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be able to use your knowledge and understanding of radioisotopes to contribute to the discussion board. Background According to our textbook, “radioisotopes are powerful tools for studying processes in biochemistry, medicine, material science, environmental studies and many other scientific and industrial fields.” Below is a list of radioisotopes and their half-lives. You will be choosing one of these radioisotopes to explore in more detail. Assignment...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be able to use your knowledge and understanding of radioisotopes to contribute to the discussion board. Background According to our textbook, “radioisotopes are powerful tools for studying processes in biochemistry, medicine, material science, environmental studies and many other scientific and industrial fields.” Below is a list of radioisotopes and their half-lives. You will be choosing one of these radioisotopes to explore in more detail. Assignment...
Over the past few weeks, you have become more familiar with your staff and are beginning...
Over the past few weeks, you have become more familiar with your staff and are beginning to feel like part of their family. To that end, you have recently accepted some "friend requests" on Facebook from some of the staff. You were at home last night, getting caught up on Facebook when you came across a post from one of the employees that said: " I hate my new boss at the clinic. A power hungry jerk making too many...
Assess the configuration of your local airport or some airport with which you can become familiar?...
Assess the configuration of your local airport or some airport with which you can become familiar? How would you describe the configuration? To what extent does it appear to meet the needs of the principal stakeholders? How flexible does it appear to be, to meet the requirements of plausible future traffic?
Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need...
Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need to create a user friendly GUI app using the code parameters of VendingChange. Turn VendingChange.java into a GUI app. 1) Your program must display the input dialog. 2) You must check that the data entered by the user follows the required input. If not, your program must display an error dialog and exit. 3) If the input is valid, then your program must display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT