data-structures · intermediate · ~15 min
Flood fill on a grid.
A rows×cols grid (row-major, 0/1). A island is a maximal group of 1s joined up/down/left/right.
int count_islands(const int *grid,int rows,int cols);
Return the number of islands.
grid rows·cols of 0/1.
Island count.
4-directional connectivity (no diagonals).
#include <stddef.h>
/* Number of connected regions of 1s (4-directional) in a rows x cols grid (row-major, 0/1). */
int count_islands(const int *grid,int rows,int cols){ (void)grid;(void)rows;(void)cols; return 0; }
Counting diagonal neighbors as connected; mutating the caller's grid (work on a copy).
All 0 → 0; all 1 → 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.