AlgoFrames

Count islands preview

Warm-up · Trace the algorithm · about 6 min

In this map, dark cells are water and light cells are land. Land cells that touch side to side belong to one island. Cells that touch only at a corner do not.

Watch the scan, then say how many islands the map has.

Example 1

Input:

{"values":[[0,1,1,0],[0,0,0,1],[1,0,0,1]]}

Output:

3

Cells (0, 2) and (1, 3) touch only at a corner.

Constraints

  • A cell joins its up, right, down and left neighbors only

Learn the idea first: Implicit graphs: grids, states and puzzles, a visual lesson.

More Implicit graphs: grids, states and puzzles problems