import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.StringTokenizer; /** Maze.java - Create a maze * (board) and start finding a solution * from the top left 0,0 position. Let's assume that * the input file name is in the command line. */ public class Maze { public static char [][] board; public static int rows, columns; public static final char wall = '0'; public static final char space = '1'; public static final char tried = '#'; public static final char path = '.'; public static final char exit = '='; public static void main(String [] args) { // Read board info from a file specified by the caller. The first line // contains the number of rows and columns of the maze. String inFileName = args[0]; Scanner inFile = null; try { inFile = new Scanner(new FileInputStream(inFileName)); } catch (FileNotFoundException e) { System.out.printf("Cannot find '%s'. Exiting...\n", inFileName); System.exit(1); } String line = inFile.nextLine(); StringTokenizer tok = new StringTokenizer(line, " "); rows = Integer.parseInt(tok.nextToken()); columns = Integer.parseInt(tok.nextToken()); board = new char[rows][columns]; for (int i = 0; i < rows; ++i) { line = inFile.nextLine(); for (int j = 0; j < columns; ++j) { board[i][j] = line.charAt(j); } } // Now that we have initialized the maze, find a way out! solve(0, 0); printBoard(); } public static boolean solve(int i, int j) { System.out.printf("solve(%2d, %2d): start\n", i, j); boolean done = false; if (blocked(i, j)) { System.out.printf("solve(%2d, %2d): we're blocked! Returning false.\n", i, j); return false; } else if (board[i][j] == exit) { System.out.printf("solve(%2d, %2d): We're done :) Returning true.\n", i, j); return true; } // In the general case, mark this square as being "tried", // and attempt to walk as far as possible in each direction n,e,s,w else { board[i][j] = tried; System.out.printf("solve(%2d, %2d): let's go up\n", i, j); done = solve(i - 1, j); if (! done) { System.out.printf("solve(%2d, %2d): let's go right\n", i, j); done = solve(i, j + 1); } if (! done) { System.out.printf("solve(%2d, %2d): let's go down\n", i, j); done = solve(i + 1, j); } if (! done) { System.out.printf("solve(%2d, %2d): let's go left\n", i, j); done = solve(i, j - 1); } } // on the way back up through the recursive returns, // mark the path we took if (done) { System.out.printf("solve(%2d, %2d): marking path\n", i, j); board[i][j] = path; } else System.out.printf("solve(%2d, %2d): need to backtrack\n", i, j); return done; } // What does it mean to be blocked? If we venture past the bounds // of the board, or run into a wall, or back up onto where we already // walked! If we forget that last check, we'd have infinite recursion. public static boolean blocked(int i, int j) { return i < 0 || j < 0 || i >= rows || j >= columns || board[i][j] == wall || board[i][j] == tried; } public static void printBoard() { for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) System.out.printf("%c", board[i][j]); System.out.printf("\n"); } } }