Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def numIslands(self, grid: List[List[str]]) -> int:
- from collections import deque
- def bfs(r, c):
- q = deque()
- visited.add((r, c))
- q.append((r, c))
- while q:
- row, col = q.popleft()
- directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
- for dr, dc in directions:
- r, c = row + dr, col + dc
- # error was here : r, c = row + dr, row + dc
- if (r) in range(rows) and (c) in range(cols) and grid[r][c] == '1' and (r ,c) not in visited:
- q.append((r , c ))
- visited.add((r, c ))
- count = 0
- visited = set()
- rows = len(grid)
- cols = len(grid[0])
- for r in range(len(grid)):
- for c in range(len(grid[0])):
- if grid[r][c] == "1" and (r,c) not in visited:
- bfs(r, c)
- count = count + 1
- return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement