Skip to content

Commit

Permalink
填充颜色
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Nov 17, 2018
1 parent 29fba14 commit f43e962
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lc733. Flood Fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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]]
"""
def dfs(pos):
image[pos[0]][pos[1]] = newColor
for d in dirs:
x, y = pos[0] + d[0], pos[1] + d[1]
if x in (-1, heigth) or y in (-1, width) or image[x][y] != color:
continue
else:
dfs((x, y))
if image[sr][sc] == newColor:
return image
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
width = len(image[0])
heigth = len(image)
color = image[sr][sc]
dfs((sr, sc))
return image


image = [
[0, 0, 0, ],
[0, 1, 1],

]
sr = 1
sc = 1
newColor = 1
s = Solution()
print(s.floodFill(image, sr, sc, newColor))

0 comments on commit f43e962

Please sign in to comment.