Question: IN JAVA: Write a program Maze.java that takes a command-line argument n, and generates a random n-by-n perfect maze. A maze is perfect if it
IN JAVA:
Write a program Maze.java that takes a command-line argument n, and generates a random n-by-n perfect maze. A maze is perfect if it has exactly one path between every pair of points in the maze, i.e., no inaccessible locations, no cycles, and no open spaces. Here's a nice algorithm to generate such mazes. Consider an n-by-n grid of cells, each of which initially has a wall between it and its four neighboring cells. For each cell (x, y), maintain a variable north[x][y] that is true if there is wall separating (x, y) and (x, y + 1). We have analogous variables east[x][y], south[x][y], and west[x][y] for the corresponding walls. Note that if there is a wall to the north of (x, y) then north[x][y] = south[x][y+1] = true. Construct the maze by knocking down some of the walls as follows:
Start at the lower level cell (1, 1).
Find a neighbor at random that you haven't yet been to.
If you find one, move there, knocking down the wall. If you don't find one, go back to the previous cell.
Repeat steps ii. and iii. until you've been to every cell in the grid.
Hint: maintain an (n+2)-by-(n+2) grid of cells to avoid tedious special cases.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
