// Project Maze // Search maze from start point (xStart, yStart) to // end point (xEnd, yEnd). Here is the search order // from the current point *: // 2 // 3 * 1 // 4 import java.io.PrintWriter; import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Scanner; public class Maze1 { public static int height, width, xStart, yStart, xEnd, yEnd; private static char[ ][ ] array; private static boolean done; public static void main(String[ ] args) throws FileNotFoundException { initialize( ); done = false; search(xStart, yStart); printResult( ); } public static void initialize() throws FileNotFoundException { Scanner s = new Scanner(new FileReader("maze.txt")); String line = ""; // Read maze from file. height = s.nextInt( ); width = s.nextInt( ); xStart = s.nextInt( ); yStart = s.nextInt( ); xEnd = s.nextInt( ); yEnd = s.nextInt( ); // Remove end of numeric input line. line = s.nextLine( ); // Instantiate and populate array. array = new char[height][width]; for(int y = 0; y <= height - 1; y++) { line = s.nextLine( ); for(int x = 0; x <= width - 1; x++) array[y][x] = line.charAt(x); } } // Recursive search routine. public static void search(int x, int y) { if (done) return; else if (x == xEnd && y == yEnd) { done = true; return; } else if (array[y][x] == ' ') { array[y][x] = 'V'; search(x + 1, y); search(x, y - 1); search(x - 1, y); search(x, y + 1); if (!done) array[y][x] = ' '; } return; } public static void printResult( ) throws FileNotFoundException { PrintWriter fw = new PrintWriter("result.txt"); // Print result. for(int y = 0; y <= width - 1; y++) { for(int x = 0; x <= height - 1; x++) fw.print(array[x][y]); fw.println( ); } fw.close( ); } }