-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_of_islands_200.py
38 lines (31 loc) · 1.2 KB
/
number_of_islands_200.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid[0])
visited = [[False for _ in range(n)] for _ in range(m)]
def valid(r, c):
return 0 <= r < m and 0 <= c < n
dirs = [(1,0),(0,1),(0,-1),(-1,0)]
def bfs(r, c):
q = deque([(r,c)])
visited[r][c] = True
while q:
row, col = q.popleft()
for d in dirs: # explore the neighbors columbus style
i = d[0] + row
j = d[1] + col
if valid(i,j):
#we can check this, so, let's add it to the q IF it's a 1, we need to see how far out this "island" goes
if grid[i][j] == '1' and not visited[i][j]:
q.append((i,j))
visited[i][j] = True
out = 0
for i in range(0,m):
for j in range(0, n):
if not visited[i][j]:
if grid[i][j] == '1':
bfs(i,j)
out +=1
else:
continue
return out