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].
 DFS Solution:
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]]
"""
R=len(image)
C=len(image[0]) oldColor=image[sr][sc]
if oldColor==newColor:
return image
def dfs(r,c):
if image[r][c]==oldColor:
image[r][c]=newColor
if r>=1:
dfs(r-1,c)
if r<R-1:
dfs(r+1,c)
if c>=1:
dfs(r,c-1)
if c<C-1:
dfs(r,c+1)
dfs(sr,sc)
return image

  

BFS Solution:

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]]
"""
R=len(image)
C=len(image[0]) oldColor=image[sr][sc]
if oldColor==newColor:
return image queue=[(sr,sc)]
while queue:
node=queue.pop(0)
if image[node[0]][node[1]]==oldColor:
image[node[0]][node[1]]=newColor
if node[0]>=1:
queue.append((node[0]-1,node[1]))
if node[0]<R-1:
queue.append((node[0]+1,node[1]))
if node[1]>=1:
queue.append((node[0],node[1]-1))
if node[1]<C-1:
queue.append((node[0],node[1]+1))
return image

  

[LeetCode&Python] Problem 733. Flood Fill的更多相关文章

  1. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...

  2. LN : leetcode 733 Flood Fill

    lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...

  3. 【LeetCode】733. Flood Fill 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  4. 733. Flood Fill 简单型染色问题

    [抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value ...

  5. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  6. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  7. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  8. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  9. [LeetCode&Python] Problem 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

随机推荐

  1. span 超出部分换行

    span{ word-break: normal; width: auto; display: block; white-space: pre-wrap; word-wrap: break-word; ...

  2. Linux 硬件信息命令

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...

  3. read later

       https://groups.google.com/forum/#!msg/pylearn-users/FYtpaQKoC4c/ubitO_JUC1kJ           网上论坛 发布回复 ...

  4. pl/sql美化

    因为这个问题曾经浪费过俺很多时间,不过今天终于发现一个小技巧,分享给大家, 在上面DDL语句前后加上begin 和 end,哈哈,再美化下试试看,DDL被成功被美化了. 具体如下: begin---① ...

  5. bzoj2330

    题解: 差分约束系统 要我们求最小值 显然就是转化为最长路 然后spfa一下即可 代码: #include<bits/stdc++.h> using namespace std; ; lo ...

  6. maven配置checkstyle插件对代码规范进行静态检查

    checkstyle配置的官方网站:http://checkstyle.sourceforge.net/config.html (1)新建maven项目,配置checkstyle插件 pom.xml ...

  7. docker 将正在运行的容器打包为镜像

    将容器打包成镜像 docker commit -a "runoob.com" -m "my apache" 容器名称或id 打包的镜像名称:标签 OPTIONS ...

  8. powershell玩转litedb数据库-第二版

    powershell可以玩nosql数据库吗?答案是肯定的.只要这个数据库兼容.net,就可以很容易地被powershell使用. 发文初衷:世界上几乎没有讲powershell调用nosql的帖子, ...

  9. 5.4 C++重载输入与输出操作符

    参考:http://www.weixueyuan.net/view/6382.html 总结: 在C++中,系统已经对左移操作符“<<”和右移操作符“>>”分别进行了重载,使其 ...

  10. SQL-29 使用join查询方式找出没有分类的电影id以及名称

    题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...