-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path200_number_of_islands.py
34 lines (24 loc) · 1009 Bytes
/
200_number_of_islands.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
from collections import deque
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
rows, cols = len(grid), len(grid[0])
visited = set()
islands = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1" and (r, c) not in visited:
self.bfs(r, c, visited, rows, cols, grid)
islands += 1
return islands
def bfs(self, row, col, visited, rows, cols, grid):
queue = deque()
queue.append((row,col))
while queue:
r, c = queue.pop()
visited.add((r, c))
directions = [[0,1], [0,-1], [1,0], [-1,0]]
for d in directions:
next_r = r + d[0]
next_c = c + d[1]
if -1 < next_r < rows and -1 < next_c < cols and grid[next_r][next_c] == "1" and (next_r, next_c) not in visited:
queue.append((next_r, next_c))