733. Flood Fill #
Problem #
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, “flood fill” the image.
To perform a “flood fill”, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
At the end, return the modified image.
Example 1:
Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.
Note:
- The length of
imageandimage[0]will be in the range[1, 50]. - The given starting pixel will satisfy
0 <= sr < image.lengthand0 <= sc < image[0].length. - The value of each color in
image[i][j]andnewColorwill be an integer in[0, 65535].
Problem Summary #
There is an image represented by a two-dimensional integer array, where each integer represents the pixel value of the image, with values ranging from 0 to 65535. You are given a coordinate (sr, sc) representing the starting pixel for rendering the image (row, column) and a new color value newColor, and you need to recolor this image.
To complete the coloring, start from the initial coordinate and record the connected pixels in the four directions up, down, left, and right whose pixel values are the same as that of the initial coordinate. Then continue recording the connected pixels that meet the condition in the four corresponding directions of those pixels, whose pixel values are also the same as that of the initial coordinate, and so on. Repeat this process. Change the color value of all recorded pixels to the new color value. Finally, return the image after the coloring has been rendered.
Note:
- The lengths of image and image[0] are in the range [1, 50].
- The given initial point will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
- The color values represented by image[i][j] and newColor are in the range [0, 65535].
Solution Approach #
- Given a two-dimensional image grid, where each point in the grid has a number. Given a starting coordinate, starting from this coordinate, color all points connected to it as newColor.
- This problem is the standard Flood Fill algorithm. It can be solved using either DFS or BFS.
Code #
package leetcode
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
color := image[sr][sc]
if newColor == color {
return image
}
dfs733(image, sr, sc, newColor)
return image
}
func dfs733(image [][]int, x, y int, newColor int) {
if image[x][y] == newColor {
return
}
oldColor := image[x][y]
image[x][y] = newColor
for i := 0; i < 4; i++ {
if (x+dir[i][0] >= 0 && x+dir[i][0] < len(image)) && (y+dir[i][1] >= 0 && y+dir[i][1] < len(image[0])) && image[x+dir[i][0]][y+dir[i][1]] == oldColor {
dfs733(image, x+dir[i][0], y+dir[i][1], newColor)
}
}
}