개발공부/algorithm

[SWEA][python] 1219.길찾기 - DFS

so.py 2021. 3. 2. 15:38

문제

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

나의 코드

  • 리스트로 들어오는 인풋을 두개의 element 별로 묶어준다.
  • 시작점 0에 대해서 재귀적으로 dfs 탐색을 진행해준다.
  • 99에 도착하면 탐색 종료
def dfs(vertex):
    global res
    # if reached end
    if vertex == 99:
        res = 1
        return

    for i in range(len(new)):
        # get the next eleme for the vertex
        if new[i][0] == vertex and not visited[new[i][0]]:
            visited[new[i][0]] = 1
            dfs(new[i][1])
            visited[new[i][0]] = 0
    
for tc in range(10):
    T, N = map(int, input().split())

    l = list(map(int, input().split()))
    # create a list containing connected vertex
    new = list(zip(l[::2], l[1::2]))
    res = 0
    # for each num
    visited = [0] * 100
    dfs(0)
    print("#{} {}".format(T, res))