문제출처:https://programmers.co.kr/learn/courses/30/lessons/43162
접근법
어제 풀었던 문제와 유사한 문제였다.
직접 연결, 간접 연결된 컴퓨터들을 하나의 네트워크로 생각하기 때문에 바로 DFS를 떠올렸다.
코드
b = [ 0 ] * 200
num , comp = 0,0
def dfs(i):
global comp,num,b
b[i] = 1
for j in range(num):
if( comp[i][j] and b[j] == 0 ):
dfs(j)
def solution(n, computers):
global comp,num,b
comp,num = computers , n
answer = 0
for j in range(n):
if( b[j] == 0 ):
answer += 1
dfs(j) # 네트워크 시작
return answer