Paint Bucket — Graph Extras
Repaint a tiny picture after a tap: the tapped pixel and every same-coloured pixel linked to it by sides take the chosen colour.
Graph Extras · Easy
The problem
An image is a grid of colour numbers. Starting at cell (sr, sc), repaint it with a new colour, and repaint every cell connected to it through up, down, left or right neighbours that have the same original colour as the starting cell. Return the repainted grid.
The story
A photo editor has a small picture of up to 5 by 5 pixels, each with a colour number from 0 to 3. The user taps one pixel with the paint bucket and picks a new colour. Every pixel that has the tapped pixel's colour and touches it by sides, directly or through other such pixels, takes the new colour. Return the picture after the tap.
What to return
- Return the grid after repainting the tapped pixel and everything connected to it by sides with the same original colour.
- Pixels touching only at a corner are not connected.
Constraints
- 1 <= rows, columns <= 5, every row has the same length
- 0 <= image[r][c] <= 3
- 0 <= sr < rows, 0 <= sc < columns
- 0 <= color <= 3
Example
- Input
image = [[1, 1, 0], [1, 0, 0], [1, 0, 1]], sr = 1, sc = 1, color = 2- Output
[[1, 1, 2], [1, 2, 2], [1, 2, 1]]