Quote:
//************************************************** ******************
// Maze3D.java CSE 110 Fall 2008
// Represents a 3D maze of characters. The goal is to get
// from the top left back corner (0,0,0)
// to the bottom right front corner (n-1,n-1,n-1),
// following a path of 1's.
//************************************************** ******************
public class Maze3D {
private final int TRIED = 3;
private final int PATH = 7;
private final int BLOCKED = 0;
private final int OPEN = 1;
private int[][][] grid = {
{ { 1, 1, 1, 0, 0 }, { 0, 0, 1, 0, 0 }, { 0, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 0 }, { 0, 1, 1, 0, 0 } },
{ { 0, 1, 0, 0, 0 }, { 0, 1, 1, 0, 0 }, { 1, 1, 1, 1, 0 },
{ 1, 1, 0, 1, 1 }, { 1, 0, 0, 0, 1 } },
{ { 1, 1, 1, 0, 0 }, { 0, 0, 1, 0, 0 }, { 0, 0, 0, 1, 0 },
{ 0, 1, 1, 1, 1 }, { 0, 1, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 0 }, { 0, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 1 }, { 0, 1, 1, 0, 0 } },
{ { 0, 0, 1, 1, 0 }, { 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1 }, { 0, 0, 1, 0, 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, int depth) {
boolean done = false;
if(traverse(row,column,depth))
{
grid[row][column][depth]= TRIED;
if(row==grid.length-1&&column==grid[0].length-1&&depth==grid[0][0].length-1)
done=true;
else
{
done=traverse(row+1,column,depth);
if(!done)
done=traverse(row,column+1,depth);
if(!done)
done=traverse(row,column,depth+1);
if(!done)
done=traverse(row-1,column,depth);
if(!done)
done=traverse(row,column-1,depth);
if(!done)
done=traverse(row,column,depth-1);
}
if (done)
grid[row][column][depth]=PATH;
}
return done;
}
// ------------------------------------------------------------------
// Determines if a specific location is valid.
// ------------------------------------------------------------------
private boolean isValid(int row, int column, int depth) {
boolean result = false;
if(row>=0 && row<grid.length && column>=0 && column<grid[row].length && depth>=0 &&
depth<grid[column].length)
if (grid[row][column][depth]==1)
result = true;
return result;
}
// Print path in sequence of coordinates (row, col, depth)
public void printPath() {
int row = 0, col = 0, depth = 0;
while (row < grid.length && col < grid[0].length
&& depth < grid[0][0].length) {
System.out.println("[" + row + ", " + col + ", " + depth + "]");
// this will temporarily marks visited path
grid[row][col][depth] *= -1;
if (isPath(row, col, depth + 1))
depth++;
else if (isPath(row, col, depth - 1))
depth--;
else if (isPath(row, col + 1, depth))
col++;
else if (isPath(row, col - 1, depth))
col--;
else if (isPath(row + 1, col, depth))
row++;
else if (isPath(row - 1, col, depth))
row--;
else {
break;
}
}
// revert back to the original (non-negative)
for (row = 0; row < grid.length; row++)
for (col = 0; col < grid[row].length; col++)
for (depth = 0; depth < grid[row][col].length; depth++)
if (grid[row][col][depth] < 0)
grid[row][col][depth] *= -1;
}
// Check if current coordinate (row, col, depth) belongs to path
private boolean isPath(int row, int col, int depth) {
boolean result = true;
if ((isPath(row,column,depth)) == PATH)
result = true;
return result;
}
// ------------------------------------------------------------------
// Returns a string representation of the maze.
// ------------------------------------------------------------------
public String toString() {
String result = "\n";
int row, column, depth;
for (row = 0; row < grid.length; row++) {
for (column = 0; column < grid[row].length; column++) {
for (depth = 0; depth < grid[row][column].length; depth++)
result += grid[row][column][depth] + "";
result += "\n";
}
result += "\n";
}
return result;
}
}
//************************************************** ******************
// MazeSearch.java Java Foundations
//
// Demonstrates recursion by solving a maze traversal.
//************************************************** ******************
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)
{
Maze3D labyrinth = new Maze3D();
System.out.println (labyrinth);
if (labyrinth.traverse (0, 0, 0)) {
System.out.println ("The maze was successfully traversed!");
labyrinth.printPath();
}
else
System.out.println ("There is no possible path.");
System.out.println (labyrinth);
}
}