题目搬运者
https://leetcode-cn.com/problems/flood-fill/
一幅图像用m × n的整数网格图像表示,其中image[i][j]表示图像的像素值。
你也被给予三个整数sr, sc和newColor。你应该从像素image[sr][sc]开始对图像进行填充。
为了执行泛洪填充,考虑起始像素,加上任意4方向上连接到与起始像素相同颜色的起始像素的像素,再加上任意4方向上连接到这些像素(同样具有相同的颜色)的像素,以此类推。用newColor替换上述所有像素的颜色
思路 广度优先算法
- 首先要有四个维度的方向坐标 [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
- 要有一个依次入队的队列
- 先push入原坐标,在push原坐标周围相同像素的四个点,并给坐标像素上色
- 在依次从queue中pop出队头坐标,在坐标周围相同像素的四个点进行判断和push操作,给坐标像素上色
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]]
"""
oldColor = image[sr][sc]