Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louis-e/8bb701318d8f478ed45e04bec52ec01e to your computer and use it in GitHub Desktop.
Save louis-e/8bb701318d8f478ed45e04bec52ec01e to your computer and use it in GitHub Desktop.
def floodFill(img, sr, sc, new_value):
old_value = img[sr][sc][0]
queue = [(sr, sc)]
seen = set()
tot_rows = img.shape[0]
tot_cols = img.shape[1]
while queue:
nxt = []
for x, y in queue:
if (img[x][y] == new_value):
continue
img[x][y] = new_value
seen.add((x,y))
if x and (x - 1, y) not in seen and img[x - 1][y] == old_value:
nxt.append((x - 1, y))
if y and (x, y- 1) not in seen and img[x][y - 1] == old_value:
nxt.append((x, y - 1))
if x < tot_rows - 1 and (x + 1, y) not in seen and img[x + 1][y] == old_value:
nxt.append((x + 1, y))
if y < tot_cols - 1 and (x, y + 1) not in seen and img[x][y + 1] == old_value:
nxt.append((x, y + 1))
queue = nxt
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment