언어: python3
문제유형: DFS/BFS
내가 푼 답안
import collections
def solution(n, computers):
answer = 0
visited = []
def connect(start):
queue = collections.deque()
queue.append(start)
while queue:
num = queue.popleft()
visited.append(num)
queue += [i for i, c in enumerate(computers[num]) if c == 1 and i not in visited]
for com in range(n):
if com not in visited:
answer += 1
connect(com)
return answer
'알고리즘 테스트' 카테고리의 다른 글
[프로그래머스/파이썬] 배달 (0) | 2021.05.06 |
---|---|
[백준 10989번] 수 정렬하기 3 (Python) (0) | 2021.02.17 |
[백준 1260번] DFS와 BFS (Python) (0) | 2021.02.06 |
[백준 1427번] 소트인사이드 (Python) (0) | 2021.01.20 |
[백준 2751번] 수 정렬하기2 (Python) (0) | 2021.01.19 |