From 773c012164822344e5ae8388ea494edfe3990d0e Mon Sep 17 00:00:00 2001 From: YoungHo Choi <0505zxc@gmail.com> Date: Fri, 25 Sep 2020 03:17:24 +0900 Subject: [PATCH] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98=EB=A8=B8?= =?UTF-8?q?=EC=8A=A4=2049189=20=EB=AC=B8=EC=A0=9C=20(=EB=AF=B8=ED=95=B4?= =?UTF-8?q?=EA=B2=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/49189/epicarts.py | 37 +++++++++++++++++++ ...70\354\240\234\355\222\200\354\235\264.md" | 11 ++++++ 2 files changed, 48 insertions(+) create mode 100644 programmers/49189/epicarts.py create mode 100644 "programmers/49189/epicarts_\353\254\270\354\240\234\355\222\200\354\235\264.md" diff --git a/programmers/49189/epicarts.py b/programmers/49189/epicarts.py new file mode 100644 index 0000000..495f57b --- /dev/null +++ b/programmers/49189/epicarts.py @@ -0,0 +1,37 @@ +from collections import deque + +def search_next_nodes(vertex, current_node): + result = [] + for v in vertex: + if current_node in v: + for i in v: + if i != current_node: + result.append(i) + vertex.remove(v) + return result + +def solution(n, vertex): + visited = [] # 방문한 노드를 저장할 공간 생성 + queue = [1] # 1부터 시작. + + while(queue): # 큐가 없을때 까지 + next_nodes = [] # 주변 노드 찾기. 중복이 없어야함. + + for current_node in queue: # 큐에 저장된 현재 노드 하나씩 꺼냄 + next_nodes.extend(search_next_nodes(vertex, current_node)) + + visited.extend(queue) # 방문한 노드 저장 + + answer = len(queue) # 정답. + + # 다음으로 탐색할 노드 찾기 + queue = [] + for i in set(next_nodes): + if not i in visited: # 방문한적이 없으면 다음 큐에 등록 + queue.append(i) + + return answer + +vertex = [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] +n = 6 +print(solution(vertex, n)) diff --git "a/programmers/49189/epicarts_\353\254\270\354\240\234\355\222\200\354\235\264.md" "b/programmers/49189/epicarts_\353\254\270\354\240\234\355\222\200\354\235\264.md" new file mode 100644 index 0000000..e1a8cb2 --- /dev/null +++ "b/programmers/49189/epicarts_\353\254\270\354\240\234\355\222\200\354\235\264.md" @@ -0,0 +1,11 @@ +# 문제 주소 +https://programmers.co.kr/learn/courses/30/lessons/49189 + +## 문제 접근 방법 +- bfs를 사용하여 너비탐색을 진행했다. +- 1의 노드를 기준으로 1단계 아래로, 2단계 아래로... 마지막까지 탐색하였다. +- 방문한적 있는 노드는 다시는 방문하지 않도록 visited에 넣었다. +- 다음 탐색할 노드는 queue에 넣었다. +- search_next_nodes 함수는 인접해 있는 다음 노드 리스트를 반환한다. + +- 그러나, 테스트케이스 7,8,9가 시간초과로 통과가 안된다.