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); + } + } + } +} +```