Devlog_by_0giru

[프로그래머스] 여행 경로 본문

[PS]

[프로그래머스] 여행 경로

0giru_kim 2021. 2. 15. 15:30
# 테스트 케이스 2 통과 / 실패

def solution(tickets):
    nation_num = len(tickets)

    graph = []
    graph_new = []
    result = []

    for inf in tickets:
        graph.append([inf[0]])

    for inf in tickets:
        for i in range(nation_num):
            if inf[0] == graph[i][0]:
                graph[i].append(inf[1])

    for inf in graph:
        if inf not in graph_new:
            graph_new.append(inf)
            
    for inf_new in graph_new:
        temp1 = inf_new[0]
        inf_new.sort()
        temp2 = inf_new.index(temp1)
        inf_new[0], inf_new[temp2] = inf_new[temp2], inf_new[0]
            
    # 초기값 ICN 입력
    result.append(graph_new[0][0])
    
    while tickets:
        for node in graph_new:
            if node[0] == result[-1]:
                depart = node[0]
                arrival = node[1]
                result.append(arrival)
                temp_num1 = graph_new.index(node)
                del graph_new[temp_num1][1]
                for ticket in tickets:
                    if ticket[0] == depart and ticket[1] == arrival:
                        temp_num2 = tickets.index(ticket)
                        del tickets[temp_num2]
            
    return result

 

테스트 케이스 2개는 통과했으나 제출시 25% 정확도 밖에 얻지 못했다.

 

dfs bfs로 문제가 분류되어있으나 이를 응용하지 못한것 같다.

 

다른 답안을 보고 공부할 예정이다.

'[PS]' 카테고리의 다른 글

[boj] 알파벳_1987  (0) 2021.02.23
[boj] 유기농 배추_1012  (0) 2021.02.18
[boj] 단지 번호 붙이기_2667  (0) 2021.02.13
[boj] 바이러스_2606  (0) 2021.02.09
[boj] DFS와 BFS_1260  (0) 2021.02.08