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

因为是2D-array的题目, 然后上下左右4个方向, 跟相对位置有关, 那么实际上跟distinct island很像, 那么很基础的做法, BFS. 此时要注意的是, 如果用BFS, 那么很有可能你的邻居后面可能再次找你, 所以记住, 要用BFS的时候, 判断需不需要加入visited 这样一个set来避免重复的判断某一个元素, 甚至无法跳出BFS的循环.

所以按照hiredintech上面的canvas的步骤, 一步一步来,

1. constraints:

1) image 的size给定了, 至少不用判断image的行或者列可能为空的情况.

2) 每个pixel的值给定了i,并且newcolor的值也给定 [0, 65535], 所以不用判断newcolor的范围.

3) image的max size给定了, 最大[50]*[50]

4) sr跟sc 默认为有效值, 很好, 不用判断了.

2. Ideas:

BFS:   assum m*n array

T:  O(m*n)   因为要扫到array的每一个元素

    S: O(m*n)  因为queue可能最多把整个array都append进去.

1) edge case, newcolor == oricolor, 直接返回原array

2) queue, 初始化将(sr,sc) 放入

3) 如果queue非空, popleft, 然后ans[nr][nc] = newcolor, visited.add(nr, nc)

4) 然后判断4个directions, 另外注意还要判断是否已经visited过了.

3) 如果邻居有效, 并且color == oricolor, 没有visited过, queue.append(邻居)

4) 返回 ans

3. code:

 class Solution:
def floodfill(self, image, sr, sc, newColor):
lr, lc, oricolor, ans = len(image), len(image[0]), image[sr][sc], image
if oricolor == newColor: return ans
queue, visited, dirs = collections.deque([(sr, sc)]), set(), [(0,1), (0, -1), (1, 0), (-1, 0)]
while queue:
pr, pc = queue.popleft()
ans[pr][pc] = newColor
visited.add((pr, pc))
for d1, d2 in dirs:
nr, nc = pr + d1, pc + d2
if 0 <= nr < lr and 0 <= nc < lc and image[nr][nc] == oricolor and (nr, nc) not in visited:
queue.append((nr, nc))
return ans

4. test cases:

1. newcolor == oricolor

2.

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

[LeetCode] 733. Flood Fill_Easy tag: BFS的更多相关文章

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

  2. [LeetCode] 490. The Maze_Medium tag: BFS/DFS

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  3. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  4. [LeetCode] 529. Minesweeper_ Medium_ tag: BFS

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  5. [LeetCode] 690. Employee Importance_Easy tag: BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  6. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  7. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  8. [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  9. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

随机推荐

  1. 【python3】爬取新浪的栏目分类

    目标地址: http://www.sina.com.cn/ 查看源代码,分析: 1 整个分类 在 div main-nav 里边包含 2 分组情况:1,4一组 . 2,3一组 . 5 一组 .6一组 ...

  2. Qt编写可拖动对象+背景地图+多种样式+多种状态(开源)

    在很多项目应用中,需要根据数据动态生成对象显示在地图上,比如地图标注,同时还需要可拖动对象到指定位置显示,能有多种状态指示,为此特意编写本控件,全部开源出来,欢迎大家提建议.同时多多支持整套自定义控件 ...

  3. vue经验 - 细节小知识点汇总(更新中...)

    1. $refs 数据这么绑定的: 然后在created中抛出来,猜猜看看打印的是两个什么? 结果如下: 第一个跑出来了,第二个,却undefiend,没有东西?!... 后来去官网查看$refs的解 ...

  4. Ubuntu 12.04 Openstack Essex 安装(单节点)

    这是陈沙克一篇非常好的博文,当时在进行openstack排错的时候,多亏了这篇文章里面有些内容 帮我找到了问题的所在: 原文:http://www.chenshake.com/ubuntu-12-04 ...

  5. 攻防对抗中常用的windows命令(渗透测试和应急响应)

    一.渗透测试 1.信息收集类 #查看系统信息 >systeminfo #查看用户信息 >net user >net user xxx #查看网络信息 >ipconfig /al ...

  6. ssh免密码登录之ssh-keygen的用法

    A服务器:192.168.100.2 B服务器:192.168.100.3 要达到的目的:A服务器ssh登录B服务器不需要输入密码 1.在A服务器下使用ssh-keygen命令生成公钥/私钥密钥对,执 ...

  7. Unity3D 面试ABC

    最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...

  8. Xcode - 打开工程,提示No Scheme解决

    错误提示,如图: 解决思路:

  9. ftp命令大全

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令.熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍之 ...

  10. Windows Server 2008 R2之五操作主控的管理

    一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...