733. Flood Fill 图像渲染

@TOC

# 题目描述

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:

1. The length of image and image[0] will be in the range [1, 50].
2. The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < 1. image[0].length.
3. The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

# 解题方法

# 方法一：DFS

``````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]]
"""
SR, SC = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r - 1, c)
if r < SR - 1: dfs(r + 1, c)
if c >= 1: dfs(r, c - 1)
if c < SC - 1: dfs(r, c + 1)
dfs(sr, sc)
return image
``````

# 方法二：BFS

``````class Solution:
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]]
"""
que = collections.deque()
que.append((sr, sc))
start = image[sr][sc]
if start == newColor: return image
M, N = len(image), len(image[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while que:
pos = que.popleft()
image[pos[0]][pos[1]] = newColor
for d in directions:
newx, newy = pos[0] + d[0], pos[1] + d[1]
if 0 <= newx < M and 0 <= newy < N and image[newx][newy] == start:
que.append((newx, newy))
return image
``````

# 日期

2018 年 2 月 28 日 2018 年 11 月 14 日 —— 很严重的雾霾