반응형
https://programmers.co.kr/learn/courses/30/lessons/43162
def solution(n:int, computers:list)-> int:
answer = 0
# 방문 기록 초기화
visited = [0 for _ in computers]
# dfs 선언
def dfs(computers:list,visited:list,start:int):
# dfs 는 재귀 안 시작 부분에서 방문 check
if visited[start] == 1 :
return
visited[start] = 1
for next in range(len(computers)): # next는 vertex 번호 의미
if visited[next] ==0 and computers[start][next] == 1: # 방문한 적 없고 연결되어 있다면...
dfs(computers,visited,next)
# 그래프가 단절되어 있음을 의미하므로... 0을 찾아서 거기서 시작
while 0 in visited:
start =visited.index(0)
dfs(computers,visited,start)
answer+=1
return answer
반응형
'ETC' 카테고리의 다른 글
[LV1] 모든 레코드 조회하기 Mysql (0) | 2020.10.03 |
---|---|
프로그래머스 DFS/BFS 단어 변환 Python 풀이 (0) | 2020.03.08 |
프로그래머스 DFS/BFS 타겟 넘버 python 풀이 (0) | 2020.03.08 |
프로그래머스 그래프 가장 먼 노드 python 풀이 (0) | 2020.03.05 |
프로그래머스 그래프 순위 python 문제풀이 (0) | 2020.03.05 |