Advertisement
MrPolywhirl

Flood-Fill

Oct 26th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. // http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/
  2. public class FloodFill0 {
  3.     // Driver program to test above function
  4.     public static void main(String[] args) {
  5.         int[][] screen = {
  6.                 { 1, 1, 1, 1, 1, 1, 1, 1 },
  7.                 { 1, 1, 1, 1, 1, 1, 0, 0 },
  8.                 { 1, 0, 0, 1, 1, 0, 1, 1 },
  9.                 { 1, 2, 2, 2, 2, 0, 1, 0 },
  10.                 { 1, 1, 1, 2, 2, 0, 1, 0 },
  11.                 { 1, 1, 1, 2, 2, 2, 2, 0 },
  12.                 { 1, 1, 1, 1, 1, 2, 1, 1 },
  13.                 { 1, 1, 1, 1, 1, 2, 2, 1 }
  14.         };
  15.  
  16.         int x = 4, y = 4, newC = 3;
  17.  
  18.         floodFill(screen, x, y, newC);
  19.         printr(screen);
  20.     }
  21.  
  22.     // A recursive function to replace previous color 'prevC' at '(x, y)'
  23.     // and all surrounding pixels of (x, y) with new color 'newC' and
  24.     private static void floodFillUtil(int arr[][], int x, int y, int prevC, int newC) {
  25.         // Base cases
  26.         if (x < 0 || x >= arr[0].length || y < 0 || y >= arr.length) return;
  27.         if (arr[x][y] != prevC) return;
  28.  
  29.         // Replace the color at (x, y)
  30.         arr[x][y] = newC;
  31.  
  32.         // Recur for north, east, south and west
  33.         floodFillUtil(arr, x + 1, y, prevC, newC);
  34.         floodFillUtil(arr, x - 1, y, prevC, newC);
  35.         floodFillUtil(arr, x, y + 1, prevC, newC);
  36.         floodFillUtil(arr, x, y - 1, prevC, newC);
  37.     }
  38.  
  39.     // It mainly finds the previous color on (x, y) and calls floodFillUtil()
  40.     public static void floodFill(int screen[][], int x, int y, int newC) {
  41.         int prevC = screen[x][y];
  42.         floodFillUtil(screen, x, y, prevC, newC);
  43.     }
  44.  
  45.     public static void printr(int[][] arr) {
  46.         for (int i = 0; i < arr.length; i++) {
  47.             for (int j = 0; j < arr[i].length; j++) {
  48.                 System.out.print(arr[i][j] + " ");
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53. }
  54.  
  55. // =================================================================
  56.  
  57. // http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/
  58. public class FloodFill1 {
  59.     // Driver program to test above function
  60.     public static void main(String[] args) {
  61.         int[][] screen = {
  62.                 { 1, 1, 1, 1, 1, 1, 1, 1 },
  63.                 { 1, 1, 1, 1, 1, 1, 0, 0 },
  64.                 { 1, 0, 0, 1, 1, 0, 1, 1 },
  65.                 { 1, 2, 2, 2, 2, 0, 1, 0 },
  66.                 { 1, 1, 1, 2, 2, 0, 1, 0 },
  67.                 { 1, 1, 1, 2, 2, 2, 2, 0 },
  68.                 { 1, 1, 1, 1, 1, 2, 1, 1 },
  69.                 { 1, 1, 1, 1, 1, 2, 2, 1 }
  70.         };
  71.  
  72.         int x = 4, y = 4, newC = 3;
  73.  
  74.         floodFill(screen, x, y, newC);
  75.         printr(screen);
  76.     }
  77.  
  78.     // A recursive function to replace previous color 'prevC' at '(x, y)'
  79.     // and all surrounding pixels of (x, y) with new color 'newC' and
  80.     private static void floodFillUtil(int arr[][], int x, int y, int prevC, int newC) {
  81.         // Base cases
  82.         if (!inBounds(arr, x, y)) return;
  83.         if (arr[x][y] != prevC) return;
  84.  
  85.         // Replace the color at (x, y)
  86.         arr[x][y] = newC;
  87.  
  88.         // Recur for north, east, south and west
  89.         floodFillUtil(arr, x + 1, y, prevC, newC);
  90.         floodFillUtil(arr, x - 1, y, prevC, newC);
  91.         floodFillUtil(arr, x, y + 1, prevC, newC);
  92.         floodFillUtil(arr, x, y - 1, prevC, newC);
  93.     }
  94.  
  95.     // It mainly finds the previous color on (x, y) and calls floodFillUtil()
  96.     public static void floodFill(int screen[][], int x, int y, int newC) {
  97.         int prevC = screen[x][y];
  98.         floodFillUtil(screen, x, y, prevC, newC);
  99.     }
  100.  
  101.     private static boolean inBounds(int[][] arr, int x, int y) {
  102.         return x >= 0 && x < arr[0].length && y >= 0 && y < arr.length;
  103.     }
  104.    
  105.     public static void printr(int[][] arr) {
  106.         for (int i = 0; i < arr.length; i++) {
  107.             for (int j = 0; j < arr[i].length; j++) {
  108.                 System.out.print(arr[i][j] + " ");
  109.             }
  110.             System.out.println();
  111.         }
  112.     }
  113. }
  114.  
  115. // =================================================================
  116.  
  117. public class FloodFill2 {
  118.     public static void main(String[] args) {
  119.         Integer[][] screen = {
  120.                 { 1, 1, 1, 1, 1, 1, 1, 1 },
  121.                 { 1, 1, 1, 1, 1, 1, 0, 0 },
  122.                 { 1, 0, 0, 1, 1, 0, 1, 1 },
  123.                 { 1, 2, 2, 2, 2, 0, 1, 0 },
  124.                 { 1, 1, 1, 2, 2, 0, 1, 0 },
  125.                 { 1, 1, 1, 2, 2, 2, 2, 0 },
  126.                 { 1, 1, 1, 1, 1, 2, 1, 1 },
  127.                 { 1, 1, 1, 1, 1, 2, 2, 1 }
  128.         };
  129.  
  130.         floodFill(screen, 4, 4, 3);
  131.         print(screen);
  132.     }
  133.  
  134.    
  135.     public static <T> void floodFill(T arr[][], int x, int y, T curr) {
  136.         floodFillUtil(arr, x, y, arr[x][y], curr);
  137.     }
  138.  
  139.     private static <T> void floodFillUtil(T arr[][], int x, int y, T prev, T curr) {
  140.         // Base cases
  141.         if (!inBounds(arr, x, y)) return;
  142.         if (!arr[x][y].equals(prev)) return;
  143.  
  144.         // Replace the color at (x, y)
  145.         arr[x][y] = curr;
  146.  
  147.         // Recur for north, east, south and west
  148.         floodFillUtil(arr, x + 1, y, prev, curr);
  149.         floodFillUtil(arr, x - 1, y, prev, curr);
  150.         floodFillUtil(arr, x, y + 1, prev, curr);
  151.         floodFillUtil(arr, x, y - 1, prev, curr);
  152.     }
  153.  
  154.     private static <T> boolean inBounds(T[][] arr, int x, int y) {
  155.         return x >= 0 && x < arr[0].length && y >= 0 && y < arr.length;
  156.     }
  157.    
  158.     public static <T> String sprint(T[][] arr) {
  159.         return join(new StringBuffer(), arr, " ").toString();
  160.     }
  161.    
  162.     public static <T> StringBuffer join(StringBuffer buffer, T[][] arr, String delim) {
  163.         for (T[] row : arr) {
  164.             join(buffer, row, delim).append(System.lineSeparator());
  165.         }
  166.         return buffer;
  167.     }
  168.    
  169.     public static <T> StringBuffer join(StringBuffer buffer, T[] arr, String delim) {
  170.         for (int i = 0; i < arr.length; i++) {
  171.             buffer.append(arr[i]);
  172.             if (i < arr.length - 1) {
  173.                 buffer.append(delim);
  174.             }
  175.         }
  176.         return buffer;
  177.     }
  178.    
  179.     public static <T> void print(T[][] arr) {
  180.         System.out.println(sprint(arr));
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement