Problem 538652 · easy · Phase 05 Advanced Algorithms & Graphs

Paint Bucket Tool

grids · BFS · flood fill

A pixel image is a grid of integers image, where image[r][c] is the colour of that pixel. The paint bucket tool is clicked on pixel (sr, sc) with a new colour colour. It recolours the clicked pixel and every pixel that can be reached from it by repeatedly stepping to a side-adjacent pixel of the same original colour. Return the modified image (modifying and returning the input grid is fine).

Examples

Input:  image = [[1,1,0],
                 [1,0,0],
                 [1,1,1]], sr = 0, sc = 0, colour = 7
Output: [[7,7,0],
         [7,0,0],
         [7,7,7]]
Explanation: the six 1-pixels form one side-connected region starting at (0,0); the 0-pixels are untouched.

Input:  image = [[2,2],[2,2]], sr = 1, sc = 1, colour = 2
Output: [[2,2],[2,2]]
Explanation: the new colour equals the old one, so nothing changes.

Constraints

  • 1 <= rows, cols <= 60, 0 <= image[r][c], colour <= 100
  • Target O(rows * cols) time.

Goals

  • Flood-fill a region of equal values with an explicit queue
  • Guard against the case where the new colour equals the old one
Starting Python…