ํ’€์ด

BFS๋กœ ํ’€์—ˆ๋Š”๋ฐ, ๋ฐฐ์—ด์ด reference๋กœ ์ „๋‹ฌ๋˜์–ด ์• ๋จน์—ˆ๋‹ค.

Code

from collections import deque
 
def solution(tickets):
    answers = []
    q = deque()
    used = [False for _ in tickets ]
    answer = ["ICN"]
    q.append(["ICN", used, answer])
    
    while q:
        start, used, answer = q.pop()
        
        if all(used):
            answers.append(answer)
        
        for i, (s, e) in enumerate(tickets):
            if s == start and used[i] == False:
                _used = used[:]
                _answer = answer[:]
                _used[i] = True
                _answer.append(e)
                q.append([e, _used, _answer])
    # print(answers.sort())
    answers.sort()
    return answers[0]

Reference