|
| 1 | +public class FloodFill { |
| 2 | + |
| 3 | + public static void floodFill(int[][] image, int sr, int sc, int newColor) { |
| 4 | + int rows = image.length; |
| 5 | + int cols = image[0].length; |
| 6 | + int oldColor = image[sr][sc]; |
| 7 | + |
| 8 | + if (oldColor != newColor) { |
| 9 | + dfs(image, sr, sc, oldColor, newColor, rows, cols); |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + public static void dfs(int[][] image, int row, int col, int oldColor, int newColor, int rows, int cols) { |
| 14 | + if (row < 0 || row >= rows || col < 0 || col >= cols || image[row][col] != oldColor) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + image[row][col] = newColor; |
| 19 | + |
| 20 | + dfs(image, row - 1, col, oldColor, newColor, rows, cols); |
| 21 | + dfs(image, row + 1, col, oldColor, newColor, rows, cols); |
| 22 | + dfs(image, row, col - 1, oldColor, newColor, rows, cols); |
| 23 | + dfs(image, row, col + 1, oldColor, newColor, rows, cols); |
| 24 | + } |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + int[][] image = { |
| 28 | + {1, 1, 1}, |
| 29 | + {1, 1, 0}, |
| 30 | + {1, 0, 1} |
| 31 | + }; |
| 32 | + |
| 33 | + int sr = 1, sc = 1, newColor = 2; |
| 34 | + |
| 35 | + floodFill(image, sr, sc, newColor); |
| 36 | + |
| 37 | + System.out.println("Flood-Filled Image:"); |
| 38 | + for (int[] row : image) { |
| 39 | + for (int pixel : row) { |
| 40 | + System.out.print(pixel + " "); |
| 41 | + } |
| 42 | + System.out.println(); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +// Description of the above code: |
| 51 | + |
| 52 | +// 1. The `FloodFill` class contains three methods: `floodFill`, `dfs`, and `main`. |
| 53 | + |
| 54 | +// 2. `floodFill` method: |
| 55 | +// - This method serves as the entry point for the flood fill algorithm. |
| 56 | +// - It takes four parameters: |
| 57 | +// - `image`: A 2D integer array representing the image where the flood fill should be performed. |
| 58 | +// - `sr` and `sc`: The starting row and column coordinates from where the flood fill should begin. |
| 59 | +// - `newColor`: The new color that should replace the old color in the connected region. |
| 60 | +// - It gets the dimensions of the `image` (number of rows and columns) and stores them in `rows` and `cols`. |
| 61 | +// - It retrieves the old color from the `image` at the starting coordinates `(sr, sc)`. |
| 62 | +// - If the old color is not equal to the new color, it calls the `dfs` method to perform the flood fill. |
| 63 | + |
| 64 | +// 3. `dfs` method: |
| 65 | +// - This is a recursive depth-first search (DFS) function that performs the actual flood fill. |
| 66 | +// - It takes six parameters: |
| 67 | +// - `image`: The 2D integer array representing the image. |
| 68 | +// - `row` and `col`: The current row and column coordinates being processed. |
| 69 | +// - `oldColor`: The old color that needs to be replaced. |
| 70 | +// - `newColor`: The new color to fill the region with. |
| 71 | +// - `rows` and `cols`: The dimensions of the image. |
| 72 | +// - It checks if the current coordinates are outside the image boundaries or if the pixel at `(row, col)` does not match the old color. In such cases, it returns, indicating that this branch of the recursion should stop. |
| 73 | +// - If the conditions are met, it updates the color at `(row, col)` to the new color. |
| 74 | +// - Then, it recursively calls `dfs` for the four neighboring pixels (up, down, left, and right), continuing the flood fill process. |
| 75 | + |
| 76 | +// 4. `main` method: |
| 77 | +// - In the `main` method, an example image represented by a 2D array `image` is defined. |
| 78 | +// - The starting row (`sr`), starting column (`sc`), and the new color (`newColor`) are specified. |
| 79 | +// - The `floodFill` method is called with these parameters to perform the flood fill. |
| 80 | +// - Finally, the modified image is printed to the console to display the result. |
| 81 | + |
| 82 | +// The code demonstrates how to use the flood fill algorithm to replace a connected region of pixels with a new color in a 2D image. |
| 83 | + |
| 84 | +/*The provided Java code implements the flood fill algorithm to change the color of a connected region in a 2D image. It starts from a given pixel and recursively fills adjacent pixels of the same original color with a new color. Here's a short description: |
| 85 | +
|
| 86 | +The `floodFill` method: |
| 87 | +- Takes an image represented as a 2D array, a starting pixel (sr, sc), and a new color. |
| 88 | +- Checks if the old color at the starting pixel is different from the new color. |
| 89 | +- If they are different, it calls the `dfs` method to perform the flood fill. |
| 90 | +
|
| 91 | +The `dfs` method: |
| 92 | +- Recursively explores neighboring pixels in four directions (up, down, left, right). |
| 93 | +- Changes the color of each visited pixel to the new color. |
| 94 | +- Stops when it encounters out-of-bounds pixels or pixels with colors different from the old color. |
| 95 | +
|
| 96 | +In the `main` method: |
| 97 | +- An example image is provided as a 2D array. |
| 98 | +- A starting point (sr, sc) and a new color are specified. |
| 99 | +- The `floodFill` method is called to fill the connected region starting from the specified point with the new color. |
| 100 | +- The resulting image is printed to the console.*/ |
| 101 | + |
0 commit comments