From 156a6a71e293fb80c9445436c2d577e60ff0e960 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Thu, 12 Jun 2025 22:41:00 +0900 Subject: [PATCH] =?UTF-8?q?[20250612]=20BOJ=20/=20G5=20/=20=EC=88=A8?= =?UTF-8?q?=EB=B0=94=EA=BC=AD=EC=A7=88=203=20/=20=EC=84=A4=EC=A7=84?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\260\224\352\274\255\354\247\210 3.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "Seol-JY/202506/12 BOJ G5 \354\210\250\353\260\224\352\274\255\354\247\210 3.md" diff --git "a/Seol-JY/202506/12 BOJ G5 \354\210\250\353\260\224\352\274\255\354\247\210 3.md" "b/Seol-JY/202506/12 BOJ G5 \354\210\250\353\260\224\352\274\255\354\247\210 3.md" new file mode 100644 index 00000000..be857a56 --- /dev/null +++ "b/Seol-JY/202506/12 BOJ G5 \354\210\250\353\260\224\352\274\255\354\247\210 3.md" @@ -0,0 +1,56 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, K; + static int[] dist = new int[100001]; + static boolean[] visited = new boolean[100001]; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + Arrays.fill(dist, Integer.MAX_VALUE); + + System.out.println(bfs()); + } + + static int bfs() { + Deque deque = new ArrayDeque<>(); + deque.offer(N); + dist[N] = 0; + + while (!deque.isEmpty()) { + int current = deque.poll(); + + if (current == K) { + return dist[current]; + } + + if (visited[current]) continue; + visited[current] = true; + + if (current * 2 <= 100000 && dist[current * 2] > dist[current]) { + dist[current * 2] = dist[current]; + deque.offerFirst(current * 2); + } + + if (current - 1 >= 0 && dist[current - 1] > dist[current] + 1) { + dist[current - 1] = dist[current] + 1; + deque.offerLast(current - 1); + } + + if (current + 1 <= 100000 && dist[current + 1] > dist[current] + 1) { + dist[current + 1] = dist[current] + 1; + deque.offerLast(current + 1); + } + } + + return dist[K]; + } +} +```