Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Model a maze in either Java or C++. Requirements: Maze must be a grid of at leas

ID: 3788212 • Letter: M

Question

Model a maze in either Java or C++.

Requirements: Maze must be a grid of at least 10 x 10 squares.

From each square, an agent (to come later) may or may not be able to move either up, down, left, or right depending on the walls in the maze.

There must be a start cell and a goal cell.

The must must be solvable.

The must must have at least one dead end.

The program must draw the maze (either as text or graphics).

One possible solution that we talked about in class is to store the maze as a 2D array. The coordinates in the array represent the positions of the squares. The values in the array represent the walls. We only need to store the presence of two walls (up and to the right, either present or not, for example) for a total of 4 possible values for each cell.

Separately store the location of the starting cell and goal cell.

Explanation / Answer

package chegg;

public class Maze {
   final int N = 10;

   void printsol(int sol[][]) {
       for (int i = 0; i < N; i++) {
           for (int j = 0; j < N; j++)
               System.out.print(" " + sol[i][j] + " ");
           System.out.println();
       }
   }

   boolean safe(int maze[][], int x, int y) {

       return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1);
   }

   boolean solveMaze(int maze[][]) {
       int sol[][] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
               { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
               { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
               { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

       if (solveM(maze, 0, 0, sol) == false) {
           System.out.print("No Solution");
           return false;
       }

       printsol(sol);
       return true;
   }

   boolean solveM(int maze[][], int x, int y, int sol[][]) {

       if (x == N - 1 && y == N - 1) {
           sol[x][y] = 1;
           return true;
       }

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

           sol[x][y] = 1;

           if (solveM(maze, x + 1, y, sol))
               return true;

           if (solveM(maze, x, y + 1, sol))
               return true;

           sol[x][y] = 0;
           return false;
       }

       return false;
   }

   public static void main(String args[]) {
       Maze mz = new Maze();
       int maze[][] = { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
               { 0, 1, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0, 0, 0, 0, 0 },
               { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
               { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 } };
       mz.solveMaze(maze);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote