import collections
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
visit = [ [False] *len(grid[0]) for i in range(len(grid))]
cnt = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if visit[i][j] == False and grid[i][j]=="1":
self.bfs(i,j,grid, visit, cnt)
cnt = cnt+1
return cnt
def bfs(self,i,j, grid,visit, cnt):
direction = [(-1,0), (1,0), (0,1), (0,-1)]
visit[i][j] == True
queue = collections.deque()
queue.append((i,j))
while queue:
x,y = queue.popleft()
for dx,dy in direction:
nx, ny = x+dx, y+dy
if nx<0 or ny <0 or nx>=len(grid) or ny>=len(grid[i]):
continue
if visit[nx][ny] == False and grid[nx][ny] == "1":
visit[nx][ny] = True
queue.append((nx,ny))
'이것저것' 카테고리의 다른 글
시스템 환경변수 cmd에서 출력하는 방법 (0) | 2022.05.14 |
---|---|
[Leet Code] 112. Path Sum (0) | 2022.03.18 |
leetcode 139. Word Break (0) | 2022.03.09 |
텐서플로우 자주쓰는 수학함수 (0) | 2022.03.01 |
관심 가져야 할 테마 (0) | 2022.02.28 |
댓글