Skip to content

Commit 33a75c4

Browse files
committed
[Add] Baekjoon > Q13549.py
1 parent 24e2b92 commit 33a75c4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Baekjoon/Q13549.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# link: https://www.acmicpc.net/problem/13549
2+
# 다시 풀어볼 것
3+
4+
from collections import deque
5+
6+
def bfs():
7+
visited = [0] * 100001 # i 노드까지 가는 데에 걸린 총 시간을 저장
8+
q = deque()
9+
q.append(N)
10+
11+
while q:
12+
cur_position = q.popleft()
13+
if cur_position == K:
14+
return visited[cur_position]
15+
for next_position in (cur_position - 1, cur_position + 1, cur_position * 2):
16+
if 0 <= next_position < 100001:
17+
if visited[next_position] == 0:
18+
if next_position == cur_position * 2 and next_position != 0: # 순간이동일 경우 (무한루프를 피하기 위해 0 조건도 넣음)
19+
visited[next_position] = visited[cur_position] # 순간이동은 0초 걸림
20+
q.appendleft(next_position) # 우선순위가 높기 때문에
21+
else:
22+
visited[next_position] = visited[cur_position] + 1 # 순간이동이 아닐 경우, 1초 걸림
23+
q.append(next_position) # 우선순위가 순간이동보다 낮음
24+
25+
N, K = map(int, input().split())
26+
print(bfs())

0 commit comments

Comments
 (0)