Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Flood Fill

Easy
graphdfsbfsmatrix

Given an image as an integer grid, a start pixel (sr, sc), and a new color, flood-fill: recolor the start pixel and every 4-directionally connected pixel sharing its original color. Return the resulting image.

Input

  • Line 1: r c
  • Next r lines: the image rows
  • Last line: three integers sr sc color

Output: the filled image, one row per line.

Example 1
Input:
3 3
1 1 1
1 1 0
1 0 1
1 1 2
Output:
2 2 2
2 2 0
2 0 1

The bottom-right 1 is not 4-connected to the start.

Example 2
Input:
2 2
0 0
0 0
0 0 0
Output:
0 0
0 0

Constraints

  • 1 ≤ r, c ≤ 50
  • 0 ≤ colors ≤ 2^16
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.