zoukankan      html  css  js  c++  java
  • 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.
    

    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].

    其实就是:

    从给定坐标开始,将与该点相邻  并且值也相同  的点的 值都换成newColor

    class Solution(object):
        def floodFill(self, image, sr, sc, newColor):
            """
            :type image: List[List[int]]
            :type sr: int
            :type sc: int
            :type newColor: int
            :rtype: List[List[int]]
            """
            old_color= image[sr][sc]
            row = len(image)
            col = len(image[0])
            
            def dfs(img, i, j):
                if i<0 or i>=row or j<0 or j>=col or img[i][j] != old_color:
                    return
                img[i][j] = newColor
                dfs(img, i+1, j)
                dfs(img, i-1, j)
                dfs(img, i, j-1)
                dfs(img, i, j+1)
                
            if old_color != newColor:
                dfs(image, sr, sc)
            return image
            

    BFS:

    class Solution(object):
        def floodFill(self, image, sr, sc, newColor):
            """
            :type image: List[List[int]]
            :type sr: int
            :type sc: int
            :type newColor: int
            :rtype: List[List[int]]
            """        
            old_color= image[sr][sc]
            if old_color == newColor:
                return image
            
            row = len(image)
            col = len(image[0])
            
            q = [(sr, sc)]
            image[sr][sc] = newColor       
            while q:
                q2 = []
                for i,j in q:                         
                    if i-1>=0 and image[i-1][j]==old_color:
                        q2.append((i-1, j))
                        image[i-1][j] = newColor       
                    if i+1<row and image[i+1][j]==old_color:
                        q2.append((i+1, j))
                        image[i+1][j] = newColor  
                    if j-1>=0 and image[i][j-1]==old_color:
                        q2.append((i, j-1))
                        image[i][j-1] = newColor  
                    if j+1<col and image[i][j+1]==old_color:
                        q2.append((i, j+1))
                        image[i][j+1] = newColor  
                q = q2
            return image
            
  • 相关阅读:
    android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
    android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
    子句判断、启动强度和去模糊化AForge.NET框架的使用(三)
    分享从网上收集的一些游戏资源,以RPG类为主
    android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
    android游戏开发框架libgdx的使用(十三)—TiledMap中的角色和角色移动
    Ajax实现评论的顶和踩功能
    Jelastic支持java的PaaS
    真心好用的VS扩展NuGet
    分享几篇文章(PDF版)
  • 原文地址:https://www.cnblogs.com/bonelee/p/8654804.html
Copyright © 2011-2022 走看看