# Untitled

unknown
python
2 years ago
1.8 kB
1
Indexable
Never
```
print("Islands")

test_grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","1","0","0"]
]
test_grid_2 = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]

visited = set()
to_be_visited: [tuple] = [(0,0)]

def unvisited_adjacents(grid: [[str]], x: int, y: int) -> set:
valids = set()
if x - 1 >= 0 and (x - 1, y) not in visited:
if x + 1 < len(grid) and (x + 1, y) not in visited:
if y - 1 >= 0 and (x, y - 1) not in visited:
if y + 1 < len(grid[0]) and (x, y + 1) not in visited:
return valids

def explore_island(grid: [[str]], x: int, y: int) -> set:
zeros = set()
print("Explore island")

while len(to_be_visited) > 0:
(cur_x, cur_y) = to_be_visited.pop()
if grid[cur_x][cur_y] == "1" and (cur_x, cur_y) not in visited:
elif grid[cur_x][cur_y] == "0" and (cur_x, cur_y) not in visited:
print(zeros)
return zeros

def islands(grid: [[str]]) -> int:
num_islands = 0
while len(to_be_visited) > 0:
(cur_x, cur_y) = to_be_visited.pop()
if grid[cur_x][cur_y] == "1":
print(f"Found island {cur_x} {cur_y}")
to_be_visited.extend(explore_island(grid, cur_x, cur_y))
num_islands += 1
else: