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...
1. Suppose that you want to program an AI agent to navigate out of a maze....
1. Suppose that you want to program an AI agent to navigate out of a maze. The agent starts at the maze’s center facing the north direction. The agent can turn to face the south, north, east, or west. The agent can be directed to move forward for a certain distance but will stop before hitting a wall. a) Formulate this as a search problem defining initial state, goal test, transition function, and cost function. b) What can you tell...
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...
To become familiar with using the image of one instrument as the object of the next...
To become familiar with using the image of one instrument as the object of the next and tracing rays through a system of multiple instruments. Multiple optics refers to any system of more than one optical instrument through which light passes. Most devices related to optics, such as cameras, microscopes, and telescopes, contain multiple optics systems. In multiple optics, the image of one optical instrument becomes the object of the next one. Thus, in multiple optics problems, you need to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT