Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-343

Enclosed Lakes Count

Problem

Count connected regions of zeros that are fully surrounded by ones.

grid is a rectangular list of rows holding only 0 and 1, and connectivity is 4-directional. A zero-region counts as a lake only when none of its cells sits on the grid border. Flood fill each unvisited region and track whether the border is touched.

Hint

A region touching any border cell drains away, so only strictly interior zero regions are lakes.

Test cases

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

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