Intermediate Level
Implement the enclaveLandCellCount method that counts land cells unable to reach the boundary.
You are given a grid where 1 represents land and 0 represents water. A land cell can move to another land cell in four directions.
Return the number of land cells that cannot reach any boundary cell. These cells are completely trapped inside the grid.
Start from all boundary land cells and mark every land cell connected to them.
Any land reached from the boundary is not an enclave because it can escape the grid. After this flood fill, count the remaining land cells that were not visited.
This approach avoids checking escape paths separately for every land cell.
Pseudocode:
function enclaveLandCellCount(grid, rows, cols):
visited = rows by cols table filled with false
queue = empty queue
add every boundary land cell to queue and mark visited
while queue is not empty:
cell = remove front
for each four-direction neighbor:
if neighbor is inside and grid value is 1 and not visited:
mark visited
add neighbor to queue
answer = 0
for each cell:
if grid[cell] == 1 and not visited[cell]:
answer++
return answer