import java.io.*; import java.util.*; public class SolveMaze { public static void main(String[] args) throws FileNotFoundException { String fileText = ""; Scanner input = new Scanner(new File("maze1.txt")); while (input.hasNextLine()) { fileText += input.nextLine() + "\n"; } Maze maze = new Maze(fileText); // maze.setAnimated(true); System.out.println(maze); System.out.println(); int startRow = 1; int startCol = 6; System.out.println("Start from (" + startRow + ", " + startCol + ")"); solve(maze, startRow, startCol); } // Finds a pathway out of the given maze from the given start location. // pre: maze != null and startRow/Col are within the maze public static void solve(Maze maze, int startRow, int startCol) { if (explore(maze, startRow, startCol)) { System.out.println(maze); } else { System.out.println("Unsolvable!"); } } public static boolean explore(Maze maze, int row, int col) { // bad: stop when we hit a wall or we've already visited something if (maze.isWall(row, col) || maze.isExplored(row, col)) { return false; } else if (row == 0 || row == maze.getHeight() - 1 || col == 0 || col == maze.getWidth() - 1) { // good: stop when we are done // (exited) return true; } else { maze.setExplored(row, col); if (explore(maze, row, col - 1) || explore(maze, row - 1, col) || explore(maze, row + 1, col) || explore(maze, row, col + 1)) { maze.mark(row, col); return true; } } return false; } }