https://leetcode.com/problems/flood-fill/description/
The length of
The given starting pixel will satisfy
The value of each color in
https://leetcode.com/problems/flood-fill/discuss/109604/Easy-Python-DFS-(no-need-for-visited)!!!
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color != newColor) dfs(image, sr, sc, color, newColor);
return image;
}
public void dfs(int[][] image, int r, int c, int color, int newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1) dfs(image, r-1, c, color, newColor);
if (c >= 1) dfs(image, r, c-1, color, newColor);
if (r+1 < image.length) dfs(image, r+1, c, color, newColor);
if (c+1 < image[0].length) dfs(image, r, c+1, color, newColor);
}
}
https://leetcode.com/problems/flood-fill/discuss/109613/JavaC++-Clean-Code
https://leetcode.com/problems/flood-fill/discuss/126058/Java-BFS-no-need-visited-matrix
We don't need visited matrix here. We can compare the originalValue and newColor. If they are same, just return the original matrix.
https://leetcode.com/problems/flood-fill/discuss/109596/Java-easy-BFS
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:
image
and image[0]
will be in the range [1, 50]
.0 <= sr < image.length
and 0 <= sc < image[0].length
.image[i][j]
and newColor
will be an integer in [0, 65535]
.
The idea is simple. Simply perform a DFS on the source cell. Continue the DFS if:
- Next cell is within bounds.
- Next cell is the same color as source cell.
There is a tricky case where the new color is the same as the original color and if the DFS is done on it, there will be an infinite loop. If new color is same as original color, there is nothing to be done and we can simply return the
image
.int color = image[sr][sc];
if (color != newColor) dfs(image, sr, sc, color, newColor);
return image;
}
public void dfs(int[][] image, int r, int c, int color, int newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1) dfs(image, r-1, c, color, newColor);
if (c >= 1) dfs(image, r, c-1, color, newColor);
if (r+1 < image.length) dfs(image, r+1, c, color, newColor);
if (c+1 < image[0].length) dfs(image, r, c+1, color, newColor);
}
}
https://leetcode.com/problems/flood-fill/discuss/109613/JavaC++-Clean-Code
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image[sr][sc] != newColor)
dfs(image, sr, sc, image[sr][sc], newColor);
return image;
}
private void dfs(int[][] image, int i, int j, int c0, int c1) {
if (i < 0 || j < 0 || i >= image.length || j >= image[0].length || image[i][j] != c0) return;
image[i][j] = c1;
dfs(image, i, j - 1, c0, c1);
dfs(image, i, j + 1, c0, c1);
dfs(image, i - 1, j, c0, c1);
dfs(image, i + 1, j, c0, c1);
}
X. BFShttps://leetcode.com/problems/flood-fill/discuss/126058/Java-BFS-no-need-visited-matrix
We don't need visited matrix here. We can compare the originalValue and newColor. If they are same, just return the original matrix.
If the originalValue is similar to newColor, we simply return the original matrix. So we don't need the visited matrix.
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image == null || image.length == 0 || image[0].length == 0) {
return new int[0][0];
}
if (sr < 0 || sr >= image.length) {
return image;
}
if (sc < 0 || sc >= image[0].length) {
return image;
}
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{sr, sc});
int originalValue = image[sr][sc];
if (originalValue == newColor) {
return image;
}
image[sr][sc] = newColor;
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!queue.isEmpty()) {
int[] cur = queue.poll();
for (int[] d : dirs) {
int x = cur[0] + d[0];
int y = cur[1] + d[1];
if (x < 0 || x >= image.length) {
continue;
}
if (y < 0 || y >= image[0].length) {
continue;
}
if (image[x][y] != originalValue) {
continue;
}
image[x][y] = newColor;
queue.offer(new int[]{x, y});
}
}
return image;
}
}
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int[] directions = new int[]{0, 1, 0, -1, 0};
int m = image.length;
int n = image[0].length;
int originalValue = image[sr][sc];
image[sr][sc] = newColor;
boolean[][] visited = new boolean[m][n];
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{sr, sc});
while (!queue.isEmpty()) {
int[] curr = queue.poll();
visited[curr[0]][curr[1]] = true;
for (int i = 0; i < directions.length - 1; i++) {
int nextR = curr[0] + directions[i];
int nextC = curr[1] + directions[i + 1];
if (nextR < 0 || nextC < 0 || nextR >= m || nextC >= n || image[nextR][nextC] != originalValue || visited[nextR][nextC]) {
continue;
}
image[nextR][nextC] = newColor;
queue.offer(new int[]{nextR, nextC});
}
}
return image;
}
multiple ways BFS is faster (level order traversal by qu.size())