Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/
- public class FloodFill0 {
- // Driver program to test above function
- public static void main(String[] args) {
- int[][] screen = {
- { 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 0, 0 },
- { 1, 0, 0, 1, 1, 0, 1, 1 },
- { 1, 2, 2, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 2, 2, 0 },
- { 1, 1, 1, 1, 1, 2, 1, 1 },
- { 1, 1, 1, 1, 1, 2, 2, 1 }
- };
- int x = 4, y = 4, newC = 3;
- floodFill(screen, x, y, newC);
- printr(screen);
- }
- // A recursive function to replace previous color 'prevC' at '(x, y)'
- // and all surrounding pixels of (x, y) with new color 'newC' and
- private static void floodFillUtil(int arr[][], int x, int y, int prevC, int newC) {
- // Base cases
- if (x < 0 || x >= arr[0].length || y < 0 || y >= arr.length) return;
- if (arr[x][y] != prevC) return;
- // Replace the color at (x, y)
- arr[x][y] = newC;
- // Recur for north, east, south and west
- floodFillUtil(arr, x + 1, y, prevC, newC);
- floodFillUtil(arr, x - 1, y, prevC, newC);
- floodFillUtil(arr, x, y + 1, prevC, newC);
- floodFillUtil(arr, x, y - 1, prevC, newC);
- }
- // It mainly finds the previous color on (x, y) and calls floodFillUtil()
- public static void floodFill(int screen[][], int x, int y, int newC) {
- int prevC = screen[x][y];
- floodFillUtil(screen, x, y, prevC, newC);
- }
- public static void printr(int[][] arr) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
- // =================================================================
- // http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/
- public class FloodFill1 {
- // Driver program to test above function
- public static void main(String[] args) {
- int[][] screen = {
- { 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 0, 0 },
- { 1, 0, 0, 1, 1, 0, 1, 1 },
- { 1, 2, 2, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 2, 2, 0 },
- { 1, 1, 1, 1, 1, 2, 1, 1 },
- { 1, 1, 1, 1, 1, 2, 2, 1 }
- };
- int x = 4, y = 4, newC = 3;
- floodFill(screen, x, y, newC);
- printr(screen);
- }
- // A recursive function to replace previous color 'prevC' at '(x, y)'
- // and all surrounding pixels of (x, y) with new color 'newC' and
- private static void floodFillUtil(int arr[][], int x, int y, int prevC, int newC) {
- // Base cases
- if (!inBounds(arr, x, y)) return;
- if (arr[x][y] != prevC) return;
- // Replace the color at (x, y)
- arr[x][y] = newC;
- // Recur for north, east, south and west
- floodFillUtil(arr, x + 1, y, prevC, newC);
- floodFillUtil(arr, x - 1, y, prevC, newC);
- floodFillUtil(arr, x, y + 1, prevC, newC);
- floodFillUtil(arr, x, y - 1, prevC, newC);
- }
- // It mainly finds the previous color on (x, y) and calls floodFillUtil()
- public static void floodFill(int screen[][], int x, int y, int newC) {
- int prevC = screen[x][y];
- floodFillUtil(screen, x, y, prevC, newC);
- }
- private static boolean inBounds(int[][] arr, int x, int y) {
- return x >= 0 && x < arr[0].length && y >= 0 && y < arr.length;
- }
- public static void printr(int[][] arr) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
- // =================================================================
- public class FloodFill2 {
- public static void main(String[] args) {
- Integer[][] screen = {
- { 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 0, 0 },
- { 1, 0, 0, 1, 1, 0, 1, 1 },
- { 1, 2, 2, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 0, 1, 0 },
- { 1, 1, 1, 2, 2, 2, 2, 0 },
- { 1, 1, 1, 1, 1, 2, 1, 1 },
- { 1, 1, 1, 1, 1, 2, 2, 1 }
- };
- floodFill(screen, 4, 4, 3);
- print(screen);
- }
- public static <T> void floodFill(T arr[][], int x, int y, T curr) {
- floodFillUtil(arr, x, y, arr[x][y], curr);
- }
- private static <T> void floodFillUtil(T arr[][], int x, int y, T prev, T curr) {
- // Base cases
- if (!inBounds(arr, x, y)) return;
- if (!arr[x][y].equals(prev)) return;
- // Replace the color at (x, y)
- arr[x][y] = curr;
- // Recur for north, east, south and west
- floodFillUtil(arr, x + 1, y, prev, curr);
- floodFillUtil(arr, x - 1, y, prev, curr);
- floodFillUtil(arr, x, y + 1, prev, curr);
- floodFillUtil(arr, x, y - 1, prev, curr);
- }
- private static <T> boolean inBounds(T[][] arr, int x, int y) {
- return x >= 0 && x < arr[0].length && y >= 0 && y < arr.length;
- }
- public static <T> String sprint(T[][] arr) {
- return join(new StringBuffer(), arr, " ").toString();
- }
- public static <T> StringBuffer join(StringBuffer buffer, T[][] arr, String delim) {
- for (T[] row : arr) {
- join(buffer, row, delim).append(System.lineSeparator());
- }
- return buffer;
- }
- public static <T> StringBuffer join(StringBuffer buffer, T[] arr, String delim) {
- for (int i = 0; i < arr.length; i++) {
- buffer.append(arr[i]);
- if (i < arr.length - 1) {
- buffer.append(delim);
- }
- }
- return buffer;
- }
- public static <T> void print(T[][] arr) {
- System.out.println(sprint(arr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement