Number of Enclaves
Number of Enclaves is LeetCode 1020 (Medium). You get an m × n grid where 0 is sea and 1 is land.
- One move goes from a land cell to the land cell directly above, below, left or right of it, or walks off the grid from a land cell on the border.
- Return how many land cells can never walk off the grid, however many moves they make.
The grid can have 250,000 cells, so each cell should be handled only a constant number of times.
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 500
- grid[i][j] is either 0 or 1.
Intuition
A land cell can walk off exactly when a path of land joins it to some land cell on the border, since the last move from that border cell leaves the grid.
So do not ask every cell whether it can escape. Start from the border instead: sink every land cell that border land can reach, and whatever land is left is trapped. One flood started from all border cells at once touches each cell at most once, so the whole grid costs O(m × n).
"Count or change the cells that cannot reach the edge" is a border flood fill: start from the boundary and work inward. Surrounded Regions uses the same flood and repaints the trapped cells instead of counting them.
Approach
Before reading on, decide where the search should start so that each cell is visited only once, rather than once for every land cell that asks whether it can escape. Then decide how a cell is marked so it is never pushed twice.
Push and sink every border land cell
Scan the first and last row and the first and last column. Each land cell there escapes in one move, so set it to 0 and push it on a stack. Sinking a cell when it is pushed means it can never be pushed a second time.
Spread inward from the stack
Pop a cell and look at its four neighbours. Each one that is inside the grid and still land can walk to this cell and then off the grid, so sink it and push it too.
Count the land that is left
When the stack is empty, add up the grid. Every remaining 1 is a land cell the flood never reached, and that sum is the answer.
Why the leftover land is exactly the enclaves
Every sunk cell was pushed by a sunk neighbour, and that chain leads back to a border cell, so each one can escape. In the other direction, if a remaining cell had a land path to the border, the flood would have moved along that path, cell by cell, and sunk it.
Code in Python, C++ and Java
Common pitfalls
Counting all land that is not on the border
return sum(grid[r][c] for r in range(1, m - 1)
for c in range(1, n - 1))flood from the border first, then count the land left
An inner cell can still escape through a chain of land. In [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] the two inner 1s connect up to the top row, so the answer is 0, not 2.
Moving diagonally
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):for nr, nc in ((r + 1, c), (r - 1, c),
(r, c + 1), (r, c - 1)):Moves are only up, down, left and right. In [[1,0,0],[0,1,0],[0,0,0]] the centre cell touches the corner only diagonally, so it is trapped and the answer is 1; with diagonal moves it wrongly becomes 0.
Recursive DFS on a big grid in Python
def dfs(r, c):
...
dfs(r + 1, c)stack = [...]
while stack:
r, c = stack.pop()A 500 × 500 grid that is all land, or a long winding strip of land, makes the recursion as deep as the strip, far past Python's default limit of 1,000 calls, and it raises RecursionError. The explicit stack has no depth limit.
Edge cases
Every cell lies on the border, so all land is sunk in the first scan and the answer is 0.
Complexity
Number of Enclaves FAQ
How is this different from Surrounded Regions and Number of Islands?
Surrounded Regions (130) runs the same border flood but rewrites the trapped cells instead of counting them. Number of Islands (200) counts groups of land, not cells, and does not care about the border.
DFS or BFS: which is better?
Neither. Swapping the stack for a queue turns this DFS into a BFS; cells are sunk in a different order, but the same set gets sunk, so the count is identical.
Can I avoid changing the input grid?
Yes. Keep a separate seen grid of booleans and count land cells that are not seen at the end. It costs another m × n cells of memory.