// Project FloodFill2 // Search maze from start point (xStart, yStart) to // end point (xEnd, yEnd). Here is the search order // from the current point *: // 4 // 3 * 2 // 1 import java.io.PrintWriter; import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Scanner; public class FloodFill2 { public static int height, width, xStart, yStart; private static char[ ][ ] array; public static void main(String[ ] args) throws FileNotFoundException { initialize( ); fill(xStart, yStart); printResult( ); } public static void initialize() throws FileNotFoundException { Scanner s = new Scanner(new FileReader("region.txt")); String line = ""; // Read maze from file. height = s.nextInt( ); width = s.nextInt( ); xStart = s.nextInt( ); yStart = 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 FloodFill routine. public static void fill(int x, int y) { if (array[y][x] == ' ') { array[y][x] = '@'; fill(x, y + 1); fill(x + 1, y); fill(x - 1, y); fill(x, y - 1); } } public static void printResult( ) throws FileNotFoundException { PrintWriter fw = new PrintWriter("filledregion.txt"); // Print result. for(int y = 0; y <= width - 1; y++) { for(int x = 0; x <= height - 1; x++) fw.print(array[y][x]); fw.println( ); } fw.close( ); } }