Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-342

Flood Fill Region Size

Problem

Measure the size of the 4-connected region of ones that contains a given cell.

grid is a rectangular list of rows holding only 0 and 1. If grid[r][c] is 0 return 0 immediately, otherwise flood fill through horizontally and vertically adjacent ones. The starting cell counts toward the size.

Hint

Guard against water at the start, then walk four directions while marking visited cells.

Test cases

case 1
in: [[[1,1,0],[1,0,1],[0,0,1]],0,0]
expected: 3
case 2
in: [[[1,1,0],[1,0,1],[0,0,1]],0,2]
expected: 0
case 3
in: [[[1,1,0],[1,0,1],[0,0,1]],2,2]
expected: 2
case 4
in: [[[1,1],[1,1]],1,1]
expected: 4
case 5
in: [[[1]],0,0]
expected: 1
Starter codePython
def flood_fill_size(grid, r, c):
    # Your code here
    pass

5 test cases · run your code against them in the editor