Scribbling

프로그래머스: 여행경로 본문

Computer Science/Coding Test

프로그래머스: 여행경로

focalpoint 2021. 11. 1. 22:17

그냥...DFS로 푼다...

from collections import defaultdict
import copy
def solution(tickets):
    tickets.sort()
    # dictionary["ICN"] = ["ATL", "SFO"]
    dictionary = defaultdict(list)
    for ticket in tickets:
        dictionary[ticket[0]].append(ticket[1])
    
    def dfs(cur, dictionary, edges, path):
        if not edges:
            return path
        for v in dictionary[cur]:
            next_dictionary = copy.deepcopy(dictionary)
            next_dictionary[cur].remove(v)
            edges.remove([cur, v])
            ret = dfs(v, next_dictionary, edges, path+[v])
            if ret:
                return ret
            edges.append([cur, v])
            
    return dfs("ICN", dictionary, tickets, ["ICN"])