[๋ฐฑ์ค-1697] ์จ๋ฐ๊ผญ์ง / Python
๐ Problem Solving/Baekjoon
https://www.acmicpc.net/problem/1697
1697๋ฒ: ์จ๋ฐ๊ผญ์ง
์๋น์ด๋ ๋์๊ณผ ์จ๋ฐ๊ผญ์ง์ ํ๊ณ ์๋ค. ์๋น์ด๋ ํ์ฌ ์ N(0 ≤ N ≤ 100,000)์ ์๊ณ , ๋์์ ์ K(0 ≤ K ≤ 100,000)์ ์๋ค. ์๋น์ด๋ ๊ฑท๊ฑฐ๋ ์๊ฐ์ด๋์ ํ ์ ์๋ค. ๋ง์ฝ, ์๋น์ด์ ์์น๊ฐ X์ผ
www.acmicpc.net
from collections import deque
n, k = map(int, input().split())
graph = [0] * 100001
dx = [-1, 1, 2]
def bfs(start):
queue = deque()
queue.append(start)
while queue:
x = queue.popleft()
if x == k:
print(graph[x])
break
for i in range(3):
if i == 2:
nx = x * dx[i]
else:
nx = x + dx[i]
if 0 <= nx < 100001 and graph[nx] == 0:
graph[nx] = graph[x] + 1
queue.append(nx)
bfs(n)
ํด์ค
์ด ๋ฌธ์ ๋ ๋ฑํ ์ด๋ ต์ง๋ ์์๋ค. ์์ ์์น๋ก๋ถํฐ -1, 1, ์์์์น*2 ๋งํผ ์ด๋ํ๋ฉด์ ๊ฐ์ ์ด์ ๊ฐ์ 1์ฉ ์ฆ๊ฐ์์ผ์คฌ๋ค.
'๐ Problem Solving > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค-11656] ์ ๋ฏธ์ฌ ๋ฐฐ์ด / Python (0) | 2021.05.18 |
---|---|
[๋ฐฑ์ค-10825] ๊ตญ์์ / Python (0) | 2021.05.17 |
[๋ฐฑ์ค-10610] 30 / Python (0) | 2021.05.17 |
[๋ฐฑ์ค-7576] ํ ๋งํ / Python (0) | 2021.05.17 |
[๋ฐฑ์ค-1475] ๋ฐฉ๋ฒํธ / Python (0) | 2021.05.17 |