-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood_fill.py
28 lines (23 loc) · 1.01 KB
/
flood_fill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
def floodFill(self, image, sr, sc, color):
# Get the original color of the starting pixel
original_color = image[sr][sc]
# If the new color is the same as the original color, no need to proceed
if original_color == color:
return image
# Define the directions (up, down, left, right)
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
# DFS function to fill the image
def dfs(x, y):
# Check bounds and if the current pixel is of the original color
if x < 0 or x >= len(image) or y < 0 or y >= len(image[0]) or image[x][y] != original_color:
return
# Change the color of the current pixel
image[x][y] = color
# Recursively check the adjacent pixels
for dx, dy in directions:
dfs(x + dx, y + dy)
# Start the flood fill from the given pixel
dfs(sr, sc)
# Return the modified image
return image