A floodfill that needs no stack, just 16 spare colors

A Stackless Floodfill Automaton

Recursive floodfill normally keeps the coordinates of every origin pixel on a stack. Nils M Holm shows that with 16 unused color values you can encode a finite state automaton directly in the image instead: each filled pixel stores the direction it came from and the direction to try next, so backtracking means following arrows rather than popping a stack. He built it for a text-based minesweeper on a BASIC system with only 7K of program memory and 30 levels of subroutine nesting, and includes a working implementation.

If you can spare 16 colors, though, no stack is needed.

More from this day

2026-10-27