Skip to content

프로그래머스 49189 문제 (미해결) #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions programmers/49189/epicarts.py
Original file line number Diff line number Diff line change
@@ -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))
11 changes: 11 additions & 0 deletions programmers/49189/epicarts_문제풀이.md
Original file line number Diff line number Diff line change
@@ -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가 시간초과로 통과가 안된다.