← Visualizers
Graphs · Boundary seeding — invert the question LeetCode 1020

Number of Enclaves / visualized

You cannot mark enclosed directly — proving a cell never reaches the edge means exploring everywhere it could go. So invert it: seed a flood from every land cell on the border, sink everything it touches, and the land still standing is exactly the land that could not walk off the board. This is the same machine as LeetCode 130 down to the last recursion; only the final line differs — 130 flips the untouched cells, 1020 counts them. Seeing that they are one problem is the thing actually being tested.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

border seed / scan cursor sinking now on the call stack enclave — counted land that escaped water
O(m·n) time · O(m·n) worst-case recursion depth  ·  the border flood is the algorithm, and it is byte-for-byte the flood in LC 130. Sinking a border-reachable cell is not destruction — it is the mark, and count += grid[r][c] then reads the complement for free. Learn the machine once and the second problem costs one line.