Asked at

Flood Fill

Easy
Verified
BFSDFSMatrix~15 min

Starting at pixel (sr, sc), flood-fill the image: recolour that pixel and every 4-directionally connected pixel sharing its original colour to color.

Return the modified image. The input arrives as a single object { image, sr, sc, color }, with image a number[][].

Examples

in{ image: [[1,1,1],[1,1,0],[1,0,1]], sr: 1, sc: 1, color: 2 }
out[[2,2,2],[2,2,0],[2,0,1]]

All 1s connected to (1,1) become 2.

in{ image: [[0,0,0],[0,1,1]], sr: 1, sc: 1, color: 1 }
out[[0,0,0],[0,1,1]]

Start colour already equals the new colour, so nothing changes.

Constraints

  • 1 ≤ rows, cols ≤ 50
  • 0 ≤ image[r][c], color < 2¹⁶
  • 0 ≤ sr < rows, 0 ≤ sc < cols

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.