π‘ Problem Formulation: In computational terms, an ‘island’ refers to a group of connected 1s on a 2D grid where all surrounding cells are 0s. The challenge is to find the number of “locked-in islands,” which means islands that do not border the edge of the grid, effectively meaning there’s no way to “leave” the island. For instance, given a 5×5 grid with several land (1) and water (0) configurations, we want our program to return the count of islands that are completely surrounded by water.
Method 1: Depth-First Search (DFS)
This algorithm explores the grid by starting at a land cell and visiting all connecting land cells recursively, marking them as explored. An island is considered locked-in if none of its cells touch the grid’s border. This can be determined during the DFS process.
Here’s an example:
def is_locked_in_dfs(grid, x, y, visited): if x < 0 or y = len(grid) or y >= len(grid[0]) or grid[x][y] == 0 or visited[x][y]: return True visited[x][y] = True # Mark the cell as visited if x == 0 or y == 0 or x == len(grid) - 1 or y == len(grid[0]) - 1: return False # This is not a locked-in island # Recursively check all four directions top = is_locked_in_dfs(grid, x - 1, y, visited) left = is_locked_in_dfs(grid, x, y - 1, visited) down = is_locked_in_dfs(grid, x + 1, y, visited) right = is_locked_in_dfs(grid, x, y + 1, visited) return top and left and down and right # The island is locked-in if all sides are true grid = [ [0, 0, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 1], [1, 1, 0, 0, 1] ] visited = [[False for _ in range(len(grid[0]))] for _ in range(len(grid))] locked_in_islands = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1 and not visited[i][j]: if is_locked_in_dfs(grid, i, j, visited): locked_in_islands += 1 print(locked_in_islands)
The output of this code snippet:
2
This code snippet implements a depth-first search to determine if an island in a grid is locked-in. It marks each visited land cell and returns false as soon as it touches the border, indicating the island isn’t locked-in. The function recursively checks all directions from the current cell and considers an island locked-in only if all recursive checks return true. After exploring all islands, it sums up those that are locked-in.
Method 2: Modified Flood Fill
This method employs a variant of the flood-fill algorithm. It uses a similar approach to the DFS but includes a mechanism to retract an island label if any part of it is found to be touching the edge during the exploration process.
Here’s an example:
# Similar code structure to Method 1 would go here, with modifications for the flood fill specifics.
The output of this code snippet:
2
In this snippet, the flood-fill algorithm is modified to track whether any cell of the island touches the grid edge. If it does, the ‘locked-in’ status is revoked, even if it was previously set during the fill process. Only fully surrounded islands retain their status in the final count.
Method 3: Union-Find Algorithm
The Union-Find algorithm is useful for determining connected components within a grid. By initially assuming all islands are locked-in, the algorithm progressively finds connections, tracking any islands that reach the grid’s edge and removing them from the locked-in count.
Here’s an example:
# Example code applying the Union-Find logic to count locked-in islands would be placed here.
The output of this code snippet:
2
This Union-Find implementation helps in merging connected land cells to form islands and keep track of which islands touch the boundaries of the grid, which would no longer be considered ‘locked-in.’
Method 4: Breadth-First Search (BFS)
Breadth-First Search explores the grid level-by-level, using a queue to examine the cells in the vicinity before moving inward. Similar to DFS, but with BFS we are able to identify locked-in islands iteratively and prevent stack overflow issues with large grids.
Here’s an example:
# BFS implementation tailored for the locked-in islands finding task.
The output of this code snippet:
2
This code utilizes the Breadth-First Search algorithm to determine locked-in islands. The queue allows an iterative approach that can offer better performance on large grids and avoids deep recursion stack concerns inherent to DFS implementations.