【leetcode】733. 图像渲染(python)+ 本地测试 (广度优先搜索 / 深度优先搜索)

【leetcode】733. 图像渲染(python)+ 本地测试 (广度优先搜索 / 深度优先搜索)_第1张图片
【leetcode】733. 图像渲染(python)+ 本地测试 (广度优先搜索 / 深度优先搜索)_第2张图片

import collections
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]]
        """
        currColor = image[sr][sc]
        if currColor == newColor:
            return image
        n, m = len(image), len(image[0])
        que = collections.deque([(sr, sc)])
        image[sr][sc] = newColor
        while que:
            x, y = que.popleft()
            for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
                if 0 <= mx < n and 0 <= my < m and image[mx][my] == currColor:
                    que.append((mx, my))
                    image[mx][my] = newColor
        return image
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1
sc = 1
newColor = 2
solution = Solution()
# solution.floodFill(image, sr, sc, newColor)
solution.floodFill([[0,0,0],[0,0,0]], 0, 0, 2)

【leetcode】733. 图像渲染(python)+ 本地测试 (广度优先搜索 / 深度优先搜索)_第3张图片

你可能感兴趣的:(Python,Leetcode,leetcode,算法,python)