Question
Modify the maze in the MazeSearch.java MazeExercise.zip ( Author: Lewis/Loftus) program, such that 1. The maze cannot be traversed. 2. There are 2 different paths
Modify the maze in the MazeSearch.java MazeExercise.zip ( Author: Lewis/Loftus) program, such that
1. The maze cannot be traversed.
2. There are 2 different paths to traverse the maze.
Do not modify any other part of the program except for the maze.
This is the original maze:
{1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1} };
After each modification, run the program and see if it works and what it does. Submit the program with the modified mazes. Write a few lines of text that explain what the program did when you ran it after modofication 1 and after modification 2.
//******************************************************************** // Maze.java Author: Lewis/Loftus // // Represents a maze of characters. The goal is to get from the // top left corner to the bottom right, following a path of 1s. //********************************************************************
public class Maze { private final int TRIED = 3; private final int PATH = 7;
private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1}, {1,0,1,1,1,0,1,1,1,1,0,0,1}, {0,0,0,0,1,0,1,0,1,0,1,0,0}, {1,1,1,0,1,1,1,0,1,0,1,1,1}, {1,0,1,0,0,0,0,1,1,1,0,0,1}, {1,0,1,1,1,1,1,1,0,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,1,1,1,1} };
//----------------------------------------------------------------- // Attempts to recursively traverse the maze. Inserts special // characters indicating locations that have been tried and that // eventually become part of the solution. //----------------------------------------------------------------- public boolean traverse(int row, int column) { boolean done = false; if (valid(row, column)) { grid[row][column] = TRIED; // this cell has been tried
if (row == grid.length-1 && column == grid[0].length-1) done = true; // the maze is solved else { done = traverse(row+1, column); // down if (!done) done = traverse(row, column+1); // right if (!done) done = traverse(row-1, column); // up if (!done) done = traverse(row, column-1); // left }
if (done) // this location is part of the final path grid[row][column] = PATH; } return done; } //----------------------------------------------------------------- // Determines if a specific location is valid. //----------------------------------------------------------------- private boolean valid(int row, int column) { boolean result = false; // check if cell is in the bounds of the matrix if (row >= 0 && row = 0 && column
// check if cell is not blocked and not previously tried if (grid[row][column] == 1) result = true;
return result; }
//----------------------------------------------------------------- // Returns the maze as a string. //----------------------------------------------------------------- public String toString() { String result = " ";
for (int row=0; row
return result; } }
*
*
*
//******************************************************************** // MazeSearch.java Author: Lewis/Loftus // // Demonstrates recursion. //********************************************************************
public class MazeSearch { //----------------------------------------------------------------- // Creates a new maze, prints its original form, attempts to // solve it, and prints out its final form. //----------------------------------------------------------------- public static void main(String[] args) { Maze labyrinth = new Maze(); System.out.println(labyrinth);
if (labyrinth.traverse(0, 0)) System.out.println("The maze was successfully traversed!"); else System.out.println("There is no possible path.");
System.out.println(labyrinth); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started