You are given a grid of integer colours with rows rows and cols columns. A paint-bucket action starts at cell (sr, sc) and, moving only up/down/left/right, recolours that cell and every cell reachable from it through cells sharing the same original colour as the start cell, painting them all with newColor.
Report how many cells actually change value. If newColor equals the start cell's original colour, nothing changes and the answer is 0.
Line 1: two integers rows and cols.
The next rows lines each contain cols integers, the grid.
The final line contains three integers sr sc newColor (0-indexed cell and the new colour).
A single integer: the number of cells whose colour changes.
Example 1
Input
3 3 1 1 0 1 0 0 0 0 1 0 0 2
Expected
3
Explanation
Start colour is 1 at (0,0). The connected 1-cells are (0,0),(0,1),(1,0). All three become 2, so 3 cells change.
Example 2
Input
2 2 5 5 5 5 0 0 5
Expected
0
Explanation
The new colour 5 equals the start colour, so nothing changes. Answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →