Flood Fill
MATRIX
BREADTH-FIRST SEARCH
DEPTH-FIRST SEARCH

Problem

Given a 2D array representing an image, where image[i][j] is the pixel value at row i and column j, perform a flood fill operation.

Starting from the pixel image[sr][sc], replace the color of this pixel and any 4-directionally connected pixels with the same initial color with color.

Return the modified image.

Examples

/* [ [2,2,0], [2,1,1], [0,1,2] ] */ floodFill([[2,2,0],[2,1,1],[0,1,2]], 1, 1, 3) // [[2,2,0],[2,3,3],[0,3,2]] /* [ [2,2,0], [2,3,3], [0,3,2] ] */ // Why? Starting from (1,1) which is color 1 // we change it and its neighboring pixels with the same color to 3. floodFill([[0,1,0],[1,1,1],[0,1,0]], 1, 1, 1) // [[0,1,0],[1,1,1],[0,1,0]] // Why? The starting pixel and its neighbors are already of color 1. // So, the image remains unchanged.
Loading...
Loading...
Loading...