leetcode 733. Flood Fill(图像渲染)

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.

给出一个图像(二维数组),给一个起点,从起点开始遍历它的上下左右四邻域,和起点相同颜色的渲染成新的颜色,并继续遍历上下左右,直至遇到和起点不同颜色时停止。

思路:
从起点开始进行一次DFS
要用一个visited数组记录下访问过的位置,而不能用颜色是否更新来判断,因为可能新的颜色和刚开始的颜色是一样的,无法判断是否已访问过,会爆掉

class Solution {
    int rows = 0;
    int cols = 0;
    int initialColor = 0;
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        rows = image.length;
        cols = image[0].length;
        boolean[][] visited = new boolean[rows][cols];
        initialColor = image[sr][sc];
        dfs(image, sr, sc, newColor, visited);
        return image;
    }
    
    void dfs(int[][] image, int sr, int sc, int newColor, boolean[][] visited) {
        if(sr < 0 || sr >= rows || sc < 0 || sc >= cols) return;
        if(visited[sr][sc]) return;
        visited[sr][sc] = true;
        if(image[sr][sc] == initialColor) {
            image[sr][sc] = newColor;
            dfs(image, sr-1, sc, newColor, visited);
            dfs(image, sr, sc+1, newColor, visited);
            dfs(image, sr+1, sc, newColor, visited);
            dfs(image, sr, sc-1, newColor, visited);
        }
        //visited[sr][sc] = false;
    }
}

还有一种方法是一开始就判断新颜色是否和起点颜色相同,相同的话就直接返回,这样就可以不用visited数组。

class Solution {
    int rows = 0;
    int cols = 0;
    int initialColor = 0;
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        rows = image.length;
        cols = image[0].length;
        
        initialColor = image[sr][sc];
        if(initialColor != newColor) dfs(image, sr, sc, newColor);
        return image;
    }
    
    void dfs(int[][] image, int sr, int sc, int newColor) {
        if(sr < 0 || sr >= rows || sc < 0 || sc >= cols) return;
        if(image[sr][sc] != initialColor) return;
                
        image[sr][sc] = newColor;
        dfs(image, sr-1, sc, newColor);
        dfs(image, sr, sc+1, newColor);
        dfs(image, sr+1, sc, newColor);
        dfs(image, sr, sc-1, newColor);
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值