Flood Fill

An image is an m x n grid of pixel values. Starting from pixel (sr, sc), recolor it and every pixel connected to it through up, down, left and right moves that has the same original color, using the new color. Return the modified image.

30:00

Flood Fill
easy
Free written solution
Topics
Companies

An image is an m x n grid of pixel values. Starting from pixel (sr, sc), recolor it and every pixel connected to it through up, down, left and right moves that has the same original color, using the new color. Return the modified image.

Example 1:
Input: {"image":[[1,1,1],[1,1,0],[1,0,1]],"sr":1,"sc":1,"color":2}
Output: [[2,2,2],[2,2,0],[2,0,1]]
Constraints:
  • m=image.lengthm = \text{image.length}, n=image[i].lengthn = \text{image[i].length}

  • 1≤m,n≤501 \leq m, n \leq 50

  • 0≤image[i][j],color<2160 \leq \text{image[i][j]}, \text{color} < 2^{16}

  • 0≤sr<m0 \leq sr < m, 0≤sc<n0 \leq sc < n

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"image":[[1,1,1],[1,1,0],[1,0,1]],"sr":1,"sc":1,"color":2}

The starting pixel (1,1) has color 1. Every pixel of color 1 connected to it will become 2.

old = 1->
new = 2

painted 0

1

1

1

1

1

S

0

1

0

1

stack (top first, 0)

empty

popped pixel
just painted
on stack
other color, stops the fill
S marks the start pixel; #k is the paint order.
Variables
VariableValue
old1
color2
painted0
stackempty
DepthFunction Call
Stack empty
0/10