1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
weeeeeb [17]
3 years ago
9

In this lab you will write a program that simulates a mouse in a maze. The maze will have one exit location. The mouse will star

t from some location in the maze and move to different locations until it gets out of the maze or gets stuck. (Your mouse will be smart enough to know that it is stuck, as long as it doesn't follow a path that contains a loop!) Sometimes the mouse will come to a dead end and will need to backtrack to try another path. No matter where the mouse is in the maze, the mouse can only travel in one of the four cardinal directions, north (up), south(down), east(right) and west(left):
Computers and Technology
1 answer:
sergij07 [2.7K]3 years ago
3 0

Answer:

/* C/C++ program to solve Rat in a Maze problem using  

backtracking */

#include <stdio.h>  

// Maze size  

#define N 4  

bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);  

/* A utility function to print solution matrix sol[N][N] */

void printSolution(int sol[N][N])  

{  

for (int i = 0; i < N; i++) {  

 for (int j = 0; j < N; j++)  

  printf(" %d ", sol[i][j]);  

 printf("\n");  

}  

}  

/* A utility function to check if x, y is valid index for N*N maze */

bool isSafe(int maze[N][N], int x, int y)  

{  

// if (x, y outside maze) return false  

if (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)  

 return true;  

return false;  

}  

/* This function solves the Maze problem using Backtracking. It mainly  

uses solveMazeUtil() to solve the problem. It returns false if no  

path is possible, otherwise return true and prints the path in the  

form of 1s. Please note that there may be more than one solutions,  

this function prints one of the feasible solutions.*/

bool solveMaze(int maze[N][N])  

{  

int sol[N][N] = { { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 } };  

if (solveMazeUtil(maze, 0, 0, sol) == false) {  

 printf("Solution doesn't exist");  

 return false;  

}  

printSolution(sol);  

return true;  

}  

/* A recursive utility function to solve Maze problem */

bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])  

{  

// if (x, y is goal) return true  

if (x == N - 1 && y == N - 1) {  

 sol[x][y] = 1;  

 return true;  

}  

// Check if maze[x][y] is valid  

if (isSafe(maze, x, y) == true) {  

 // mark x, y as part of solution path  

 sol[x][y] = 1;  

 /* Move forward in x direction */

 if (solveMazeUtil(maze, x + 1, y, sol) == true)  

  return true;  

 /* If moving in x direction doesn't give solution then  

 Move down in y direction */

 if (solveMazeUtil(maze, x, y + 1, sol) == true)  

  return true;  

 /* If none of the above movements work then BACKTRACK:  

  unmark x, y as part of solution path */

 sol[x][y] = 0;  

 return false;  

}  

return false;  

}  

// driver program to test above function  

int main()  

{  

int maze[N][N] = { { 1, 0, 0, 0 },  

    { 1, 1, 0, 1 },  

    { 0, 1, 0, 0 },  

    { 1, 1, 1, 1 } };  

solveMaze(maze);  

return 0;  

}  

You might be interested in
With a(n) ____ structure, you perform an action or task, and then you perform the next action in order.
e-lub [12.9K]
The answer is a <u>sequence structure</u>
8 0
3 years ago
What output is produced by the following code? Integer first = new Integer(7); Integer second = first; if (first == second) Syst
fenix001 [56]

Answer:

The correct output of this question is  "Sneezy Sleepy".

Explanation:

In the given code firstly we create the object of the integer class that is first in this we pass the integer value that is 7. Then we declare another second integer variable is this variable we pass object as a reference. Then we use the two conditional statements. In the first, if block we check that the object of the class is equal to the reference variable. If it is true it prints Sneezy. otherwise, it will print Grumpy. In second if block we check that the object of the class is equal to the reference variable. but in this time we the equals() function it is the same as(==). If the condition is true it prints Sleepy. otherwise, it will print Doc. So the output of the code is Sneezy Sleepy.

6 0
3 years ago
Write a program that asks the user to input an integer named numDoubles. Create a dynamic array that can store numDoubles double
Hitman42 [59]

Answer:

Answered in C++

#include <iostream>

using namespace std;

int main() {

   int size;

   cout<<"Length of array: ";

   cin >> size;

   double *numDoubles = new double[size];

   double sum =0;

   cout<<"Array Elements: ";

   for(int i=0;i<size;i++){

       cin>>numDoubles[i];

       sum += numDoubles[i];

   }

   delete [] numDoubles;

   cout<<"Average: "<<sum/size;

   return 0;  

}

Explanation:

This line declares the size (or length) of the array

<em>    int size; </em>

This line prompts the user for the array length

<em>    cout<<"Length of array: "; </em>

This gets the input from the user

<em>    cin >> size; </em>

This dynamically declares the array as of double datatype

<em>    double *numDoubles = new double[size]; </em>

This declares and initializes sum to 0

<em>    double sum =0; </em>

This prompts the user for elements of the array

<em>    cout<<"Array Elements: "; </em>

The following iteration gets input from the user and also calculates the sum of the array elements

<em>    for(int i=0;i<size;i++){ </em>

<em>        cin>>numDoubles[i]; </em>

<em>        sum += numDoubles[i]; </em>

<em>    } </em>

This deletes the array

<em>    delete [] myarray; </em>

This calculates and prints the average of the array

<em>    cout<<"Average: "<<sum/size; </em>

6 0
3 years ago
Is the ipv6 address 2001:1d5::30a::1 a valid address? why or why not?
defon
<span>Not a valid IPv6 address A valid IPv6 address consist of 8 groups of 4 hexadecimal numbers separated by colons ":". But that can make for a rather long address of 39 characters. So you're allowed to abbreviate an IPv6 address by getting rid of superfluous zeros. The superfluous zeros are leading zeros in each group of 4 digits, but you have to leave at least one digit in each group. The final elimination of 1 or more groups of all zeros is to use a double colon "::" to replace one or more groups of all zeros. But you can only do that once. Otherwise, it results in an ambiguous IP address. For the example of 2001:1d5::30a::1, there are two such omissions, meaning that the address can be any of 2001:1d5:0:30a:0:0:0:1 2001:1d5:0:0:30a:0:0:1 2001:1d5:0:0:0:30a:0:1 And since you can't determine which it is, it's not a valid IP address.</span>
6 0
4 years ago
PLEASE HELP!!! WILLGIVE BRAINLIEST!!!
morpeh [17]
Black absorbs all visible parts of the spectrum turning that light energy into heat. The more it absorbs the more heat it emits.
4 0
3 years ago
Read 2 more answers
Other questions:
  • What kind of website uses keywords to locate content?
    15·1 answer
  • What is one reason the number of DUIs has dropped?
    7·1 answer
  • use the internet to research the SYSTEM account. Why is it nessesary to include this account with full control on a directory
    13·1 answer
  • E-banking is also called: [1]
    9·1 answer
  • What do you understand by storage devices ? Name any two storage devices.​
    11·2 answers
  • Another one please help quick i got to go soon
    7·1 answer
  • What are the benefits and risks of a client-server network?
    5·1 answer
  • Create a program which reads in CSV data of Creatures and loads them into aHash Map. The key of this hash map will be the name o
    10·1 answer
  • Choose the best collection for each situation.
    8·1 answer
  • Tim is a project manager. He is brainstorming with his team about problems that could arise during the next phase of the project
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!