zoukankan      html  css  js  c++  java
  • 1062. 洪水填充(经典)

    1062. 洪水填充

    中文English

    一张image通过一个2-D整数数组来表示,每一个整数代表图片的像素值(从0到65535)。

    给定一个坐标 (sr, sc) 代表洪水填充的起始像素(行和列),以及一个像素颜色newColor,“洪水填充”整张图片。

    为了实现一个“洪水填充”,考虑起始像素点,以及与起始像素相同颜色的起始像素4向连接的任何像素,以及与这些像素4向相连的任何像素(同样和起始像素相同颜色),以此类推。把所以之前提到的像素点替换成新的颜色。

    最终,返回修改后的图片。

    样例

    样例 1:

    输入: 
    image = [[1,1],[0,0]]
    sr = 0, sc = 0, newColor = 2
    输出: [[2,2],[0,0]]
    

    样例 2:

    输入:
    image = [[1,1,1],[1,1,0],[1,0,1]]
    sr = 1, sc = 1, newColor = 2
    输出: [[2,2,2],[2,2,0],[2,0,1]]
    解释:
    从图片的中心(坐标为(1, 1)),所有和起始像素通过相同颜色相连的像素上成了新的颜色。
    注意底下的角落没有被染成2,因为它和起始像素不是4方向相连。
    

    注意事项

    • image 和 image[0]的长度会在[1, 50]范围内。
    • 给定的起始像素点会满足 0 <= sr < image.length 和 0 <= sc < image[0].length
    • image[i][j]的每一中颜色值和newColor 会是在 [0, 65535]内的整数。
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param image: a 2-D array
        @param sr: an integer
        @param sc: an integer
        @param newColor: an integer
        @return: the modified image
        """
        ''' 
        大致思路:
        1.给出一个方法,可以求得当前像素相同颜色的像素,4方向相连,返回未添加的像素点列表。
        2.初始化res,result = [],[],循环image,首先append到res初始值,image[sr][sc],然后调用方法,传入参数,最终得到所有4向相连的列表组合。
        3.result作为一个循环添值减值的列表,所以这个作为循环判断条件,如果pop完所有的值,则break
        '''
    
        def floodFill(self, image, sr, sc, newColor):
            res = []
            #给定初始值
            origin_color = image[sr][sc]
            res,result = [[sr,sc]],[[sr,sc]]
    
    
            while  result != []:
                pop_column = result.pop(0)
                image[pop_column[0]][pop_column[1]] = newColor
                #pop_column格式:[x,y]
                r_list = self.getaroundcolumn(image,pop_column,origin_color,res)
                res.extend(r_list)
                result.extend(r_list)
    
            return image
    
    
    
        ##得到当前像素四向相连的像素列表集合
        def getaroundcolumn(self,image,num,origin_color,res):
            around_list = []
            x,y = num[0],num[1]
            ##不管是哪个元素,进来都判断4个点,即上下左右四个像素
            '''
            符合条件,则append到self.res里面
            1.x,y范围在[0,len(image)]和[0,len(image[0])]
            2.当前像素不存在self.res里面
            '''
    
            #左方向:i不变,y-1
            if y-1 >= 0:
                if [x,y-1] not in res and image[x][y-1] == origin_color:
                    around_list.append([x,y-1])
            
            #右方向
            if y + 1 < len(image[0]):
                if [x,y+1] not in res and image[x][y+1] == origin_color:
                    around_list.append([x,y+1])
    
            #上方向
            if x-1 >= 0:
                if [x-1,y] not in res and image[x-1][y] == origin_color:
                    around_list.append([x-1,y])
            
            #下方向
            if x+1 < len(image):
                if [x+1,y] not in res and image[x+1][y] == origin_color:
                    around_list.append([x+1,y])
            return around_list
  • 相关阅读:
    關于招聘新人
    JS在线打字练习 PHP
    useragent 分析 PHP
    webSql工具 PHP
    《网站开发人员应该知道的61件事》[解读] PHP
    HTMLCSS速查 PHP
    Flash文字转图片 PHP
    Flash简易文件上传 PHP
    Google 字体 API PHP
    Google 二维条码 API PHP
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12641948.html
Copyright © 2011-2022 走看看