Flood Fill

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write floodFill(image, row, col, color). image is a grid of numbers: an array of rows, all the same length. Recolour the cell image[row][col] to color, along with every cell joined to it up, down, left or right through cells of its starting colour, and return the result as a new grid. Don't change image. Cells that touch only at a corner aren't joined, and if color is already the starting colour, nothing changes. For example, filling [[1, 1, 1], [1, 1, 0], [1, 0, 1]] from row 1, column 1 with 2 gives [[2, 2, 2], [2, 2, 0], [2, 0, 1]]: the bottom-right 1 touches the region only at a corner.

Your code must pass

  • Fills the region joined to the start cell
  • Leaves cells that touch only at a corner
  • Handles a colour that's already the starting colour
  • Returns a new grid and leaves image unchanged
  • Handles a single cell and a single row