Skip to content

Instantly share code, notes, and snippets.

@scwood
Last active June 5, 2018 22:06
Show Gist options
  • Select an option

  • Save scwood/0382191916798f0af4f6e6e91d32af07 to your computer and use it in GitHub Desktop.

Select an option

Save scwood/0382191916798f0af4f6e6e91d32af07 to your computer and use it in GitHub Desktop.
Paint bucket problem
def paint_bucket(image, row, col, new_color):
original_color = image[row][col]
_paint(image, row, col, new_color, original_color)
def _paint(image, row, col, new_color, original_color):
if (row < 0 or
row > len(image) - 1 or
col < 0 or
col > len(image[row]) - 1 or
image[row][col] != original_color):
return
image[row][col] = new_color
_paint(image, row + 1, col, new_color, original_color)
_paint(image, row - 1, col, new_color, original_color)
_paint(image, row, col + 1, new_color, original_color)
_paint(image, row, col - 1, new_color, original_color)
image = [
['B', 'G', 'B', 'B', 'B'],
['G', 'G', 'B', 'B', 'B'],
['G', 'G', 'G', 'B', 'B'],
['B', 'B', 'G', 'B', 'B'],
['B', 'B', 'G', 'B', 'B'],
]
paint_bucket(image, 0, 1, 'R')
print(image)
# [['B', 'R', 'B', 'B', 'B'],
# ['R', 'R', 'B', 'B', 'B'],
# ['R', 'R', 'R', 'B', 'B'],
# ['B', 'B', 'R', 'B', 'B'],
# ['B', 'B', 'R', 'B', 'B']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment