diff --git "a/alstjr7437/BFS/\354\210\250\353\260\224\352\274\255\354\247\210.py" "b/alstjr7437/BFS/\354\210\250\353\260\224\352\274\255\354\247\210.py" new file mode 100644 index 0000000..706325e --- /dev/null +++ "b/alstjr7437/BFS/\354\210\250\353\260\224\352\274\255\354\247\210.py" @@ -0,0 +1,28 @@ +from collections import deque + +n, k = map(int, input().split()) + +result = [0] * 100001 +result[n] = 1 + +queue = deque() +queue.append(n) + +while queue: + now = queue.popleft() + if now == k: + break + # -1 로직 + if now - 1 >= 0 and result[now-1] == 0 : + queue.append(now-1) + result[now-1] = result[now] + 1 + # +1 로직 + if now + 1 < len(result) and result[now+1] == 0: + queue.append(now+1) + result[now+1] = result[now] + 1 + # *2 로직 + if now * 2 < len(result) and result[now * 2] == 0: + queue.append(now*2) + result[now*2] = result[now] + 1 + +print(result[k] - 1) \ No newline at end of file diff --git a/alstjr7437/README.md b/alstjr7437/README.md index bff4515..edb08f4 100644 --- a/alstjr7437/README.md +++ b/alstjr7437/README.md @@ -20,4 +20,5 @@ | 16차시 | 2024.03.13 | 정렬 | 선 긋기 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/57 | | 17차시 | 2024.03.16 | DP | 구간 나누기 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/61 | | 18차시 | 2024.03.23 | 다익스트라 | 최단 경로 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/66 | -| 19차시 | 2024.03.27 | 문자열 | IOIOI | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/67 | \ No newline at end of file +| 19차시 | 2024.03.27 | 문자열 | IOIOI | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/67 | +| 20차시 | 2024.04.03 | BFS | 숨바꼭질 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/70 | \ No newline at end of file