DFS-경로모두찾기

Equeue·2021년 11월 22일

-그래프에서 해당 노드로 가는 모든 경로의 경우 찾기

def dfs(node,n,computers,root,root_list):
    
    root.append(node)
    print(node,root)
    #해당 컴퓨터 (행) 에 1인 value의 index 방문
    
    if node==2:
        print(root)
        root_list.append(root.copy())
        return
    
    #연결된 computer가 있고, 방문하지 않았다면 방문
    #
    for c in range(0,n):#현재노드를 방문하지 않고, 무한순환되지 않도록 경로에 포함되지 않은곳, 연결된 노드인곳 을 방문
        if node!=c and (c not in root) and computers[node][c]==1:
            
            dfs(c,n,computers,root,root_list)
            #print(root_list)
            root.pop()
            print("f",node,c,root)
            #마지막에 root는 결국 [] 아무것도 남지 않게됨.

def solution(n, computers):
    answer = 0
    visited_map=[False for col in range(n)]
    root=[]
    root_list=[]
    #0에서 시작
    #dfs(현재노드, 노드갯수, 연결상태, 노드방문순서,목표노드방문한 가능한 방문순서 담은 배열)
    dfs(0,n,computers,root,root_list)

    print("r_l",root_list)

    return answer

print(solution(4,[[1,1,0,1],[1,1,0,1],[0,0,1,1],[1,1,1,1]]))
profile
Equeue's Develop Post

0개의 댓글