FloodFill [source code]

public class FloodFill {
static
/******************************************************************************/
class Solution {
    static int[][] offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if (image[sr][sc] == newColor)
            return image;
        dfs (image, sr, sc, image[sr][sc], newColor);
        return image;
    }

    void dfs (int[][] image, int x, int y, int oldColor, int newColor) {
        if (x < 0 || y < 0 || x >= image.length || y >= image[0].length || image[x][y] != oldColor )
            return;
        image[x][y] = newColor;
        for (int[] offset : offsets)
            dfs (image, x + offset[0], y + offset[1], oldColor, newColor);
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        FloodFill.Solution tester = new FloodFill.Solution();
        int[][][] inputs = {
            {{1,1,1},{1,1,0},{1,0,1}}, {{1}, {1}, {2}}, {{2,2,2},{2,2,0},{2,0,1}}
        };
        for (int i = 0; i < inputs.length / 3; i++) {
            System.out.println (Printer.separator ());
            int[][] image = inputs[3 * i];
            int sr = inputs[3 * i + 1][0][0], sc = inputs[3 * i + 1][1][0], newColor = inputs[3 * i + 1][2][0];
            int[][] ans = inputs[3 * i + 2];
            String ansStr = Matrix.printMatrix (ans);
            String inputStr = Matrix.printMatrix (image);
            // Careful with the mutation of IMAGE: use string to store snapshot
            int[][] output = tester.floodFill (image, sr, sc, newColor);
            String outputStr = Matrix.printMatrix (output);
            System.out.printf ("%s and {%d, %d, %d} -> \n%s, expected: \n%s\n",
                inputStr, sr, sc, newColor, Printer.wrapColor (outputStr, outputStr.equals (ansStr) ? "green" : "red"), ansStr
            );
        }
    }
}

题目本身没有什么不好理解的地方, 直接写; flooding问题记得要检查边界; 题目给了range, 所以不用担心爆栈; dfs flooding问题还要注意的一个地方, 就是采取什么order;

最后很简单的一个题目, 不过最后还是花了20分钟才AC, 很丢人; 速度是20ms (71%), 可以接受;


editorial:

class Solution {  
    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);  
        }  
    }  
}

还是很类似的一个算法, 不过跟我之前自己解释过的一个问题一样, 他这里这个答案采用的是在preleaf的位置检查bound, 我的做法是在leaf的位置检查bound;

另外就是negative return跟negative no-op两种写法风格的取舍, 这个在写Pintos的时候其实也是见了很多次了;


discussion没有什么其他的解法, submission基本也是波动;


Problem Description

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 image and image[0] will be in the range [1, 50].
  • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
  • The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

Difficulty:Easy
Total Accepted:6.3K
Total Submissions:13K
Contributor:jatermelon
Companies
uber
Related Topics
depth-first search
Similar Questions
Island Perimeter

Hide Hint 1

Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.

results matching ""

    No results matching ""