DFS-네트워크(프로그래머스)

Equeue·2021년 11월 22일

프로그래머스 Level3 네트워크 문제 풀이

#프로그래머스 dfs 
#level3 네트워크 

import sys
sys.setrecursionlimit(100000)

def dfs(node,n,computers,visited_map):
    #해당 컴퓨터 (행) 에 1인 value의 index 방문
    visited_map[node]=True
    
    print(visited_map)
    
    #연결된 computer가 있고, 방문하지 않았다면 방문
    #
    for c in range(0,n):#부모 node는 제외 해도 되므로 1부터 시작가능.
        if visited_map[c]==False and computers[node][c]==1:
            
            dfs(c,n,computers,visited_map)
            #110
            #110
            #001



def solution(n, computers):
    answer = 0
    visited_map=[False for col in range(n)]
    
    #모든 computer방문 
    for node in range(n):
        #방문안한곳 dfs 호출
        
        if(visited_map[node]==False):
            dfs(node,n,computers,visited_map)
            answer+=1
            print(answer)
        #dfs로 방문한 computer와 연결된 모든 computer 방문하고 방문표시.
        #그럼 언제 network count가 올라감?
        #dfs가 호출될때마다 count가 올라감.

    return answer

print(solution(3,[[1, 1, 0], [1, 1, 0], [0, 0, 1]]))
print(solution(3,[[1, 1, 0], [1, 1, 1], [0, 1, 1]]))




profile
Equeue's Develop Post

0개의 댓글