문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/43162
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
위 문제의 경우, 모든 경우의 수를 탐색하기 때문에, DFS 알고리즘을 선택했습니다.

코드 구현
from collections import defaultdict
def dfs(computers, visited, node):
visited[node] = True
for idx, connected in enumerate(computers[node]):
# 연결되어있는데 방문하지 않은 노드일 경우
if connected and not visited[idx]:
dfs(computers, visited, idx)
def solution(n, computers):
answer = 0 # DFS 함수 호출 횟수
visited = [False] * n # 방문 여부를 저장하는 리스트
for i in range(n):
if not visited[i]:
dfs(computers, visited, i)
answer +=1
return answer
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 섬 연결하기 (Python) - 유니온 파인드 알고리즘 (0) | 2025.04.10 |
---|---|
[프로그래머스] 게임 맵 최단거리 (Python) - BFS (0) | 2025.04.07 |