Number of Enclaves
WHAT IT SAYS
Count the land cells from which you cannot walk off the boundary of the grid.
WHAT IT'S REALLY ASKING
"Trapped land = all land minus escapable land — and escapable land is one flood fill from the border. When a count is over a hard-to-verify set, don't count its members one by one; count the easy complement and subtract. The border walk you'd do anyway becomes the entire algorithm, with arithmetic doing the rest."
Per-region escape trials
O(m·n) aggregate — but with per-region verdict state and size accumulatorsIdentify each connected land region; traverse it while checking whether any of its cells lies on the border. Regions that never touch the border contribute their full size to the answer; sum those sizes.
WHERE THE WORK IS WASTED — Every region pays for a trial it may not need: interior regions get fully traversed AND fully counted, border-touching regions get fully traversed only to have their count discarded — and each traversal drags along two pieces of deferred state (an escaped flag and a running size) whose interplay is the bug surface: count-then-cancel, or flag-then-skip, either way the verdict arrives after the work. The question 'how big is the trapped set?' is being answered by interrogating each candidate individually, when a single subtraction — total land minus escaping land — answers it wholesale, with the escaping set computable from seeds the problem hands you free of charge.
Count the complement once; subtraction does the counting.
The identity that dissolves the problem: enclave cells = (all land cells) − (land cells reachable from the border). It holds because 'can walk off the grid' and 'connected to some border land cell' are the same property — a walk off the edge must pass through a border cell, and connectivity carries escape to every cell of a region. So the trapped set is exactly the complement, within land, of the border-reachable set. Both terms on the right are cheap in a way the left side is not. Total land: one scan, one counter — no structure at all. Border-reachable land: one multi-seed flood fill launched from every land cell on the edge, sinking (or marking) everything it touches — the Surrounded Regions engine, reused verbatim. Neither term involves per-region verdicts; neither defers anything. Two orderings of the arithmetic, equally valid, choose by taste: (a) count all land first, then flood-sink from the border while decrementing the counter per sunk cell — the counter IS the answer when the fill finishes; (b) flood-sink from the border first (no counting), then scan and count surviving land — survivors are enclaves by elimination. Version (b) is two clean phases with zero shared state, which is its argument. The deeper habit, stated once for this pair of problems: Surrounded Regions asked WHICH cells are trapped (a classification, needing the trichotomy sweep); Enclaves asks HOW MANY (a measure). Measures unlock the subtraction shortcut — you never need to identify the trapped cells individually, only to size their complement — which is why (a) can finish without ever knowing where a single enclave is. When a problem downgrades from 'find them' to 'count them', check whether |A| = |U| − |Aᶜ| lets the easy complement carry the whole computation.
Sink from the border, count what remains
O(m·n) time — one fill plus one sweep — O(m·n) worst-case frontierFor each border cell holding land, flood fill (iterative DFS/BFS) turning reached land to water. Then one sweep counting land cells — each survivor is provably unreachable from the border. Or: pre-count land, decrement during the sink, return the counter.
Size the Complement, Subtract
YOU'LL SEE IT AGAIN WHEN
- The question asks for a COUNT (not identification) over a set defined by unreachability — a universal negative per element.
- The complement set is one propagation from explicitly given seeds (border cells, exits), making its size nearly free.
- Per-region trials would compute sizes and verdicts separately, then reconcile — deferred state the subtraction never needs.