From 3ac12bc7299115e61673db6113709d3976f9ed83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=8B=A0=EC=A7=80?= <101992179+ksinji@users.noreply.github.com> Date: Tue, 25 Nov 2025 16:51:09 +0900 Subject: [PATCH] =?UTF-8?q?[20251125]=20BOJ=20/=20G5=20/=20=EC=88=A8?= =?UTF-8?q?=EB=B0=94=EA=BC=AD=EC=A7=88=203=20/=20=EA=B0=95=EC=8B=A0?= =?UTF-8?q?=EC=A7=80?= 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" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "ksinji/202511/26 BOJ \354\210\250\353\260\224\352\274\255\354\247\210 3.md" diff --git "a/ksinji/202511/26 BOJ \354\210\250\353\260\224\352\274\255\354\247\210 3.md" "b/ksinji/202511/26 BOJ \354\210\250\353\260\224\352\274\255\354\247\210 3.md" new file mode 100644 index 00000000..6c549b8b --- /dev/null +++ "b/ksinji/202511/26 BOJ \354\210\250\353\260\224\352\274\255\354\247\210 3.md" @@ -0,0 +1,52 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int k = Integer.parseInt(st.nextToken()); + + if (n >= k) { + System.out.print(n-k); + return; + } + + Deque dq = new ArrayDeque<>(); + int[] time = new int[100001]; + Arrays.fill(time, Integer.MAX_VALUE); + time[n] = 0; + dq.add(n); + + while(!dq.isEmpty()){ + int cur = dq.pollFirst(); + + if (cur == k) { + System.out.println(time[cur]); + return; + } + + int next = cur * 2; + if (next <= 100000 && time[next] > time[cur]) { + time[next] = time[cur]; + dq.addFirst(next); + } + + next = cur - 1; + if (next >= 0 && time[next] > time[cur] + 1) { + time[next] = time[cur] + 1; + dq.addLast(next); + } + + next = cur + 1; + if (next <= 100000 && time[next] > time[cur] + 1) { + time[next] = time[cur] + 1; + dq.addLast(next); + } + } + } +} +```