Untitled
unknown
plain_text
3 years ago
862 B
7
Indexable
from collections import deque
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
num_rows = len(grid)
num_cols = len(grid[0])
count=0
moves=[[-1,0],[1,0],[0,1],[0,-1]]
def bfs(row,col):
queue=deque([(row,col)])
grid[row][col]='0'
while queue:
x,y=queue.popleft()
for dx,dy in moves:
tempX=x+dx
tempY=y+dy
if 0<=tempX<num_rows and 0<=tempY<num_cols and grid[tempX][tempY]=='1':
queue.append((tempX,tempY))
grid[tempX][tempY]='0'
for i in range(num_rows):
for j in range(num_cols):
if grid[i][j]=='1':
bfs(i,j)
count+=1
return count
Editor is loading...