diff --git "a/suyeun84/202506/17 BOJ G4 \354\210\250\353\260\224\352\274\255\354\247\210 2.md" "b/suyeun84/202506/17 BOJ G4 \354\210\250\353\260\224\352\274\255\354\247\210 2.md" new file mode 100644 index 00000000..c589b7f0 --- /dev/null +++ "b/suyeun84/202506/17 BOJ G4 \354\210\250\353\260\224\352\274\255\354\247\210 2.md" @@ -0,0 +1,49 @@ +```java +import java.io.*; +import java.util.*; + +public class boj12851 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N, K; + static int minTime = Integer.MAX_VALUE; + static int cnt = 0; + static int[] visited = new int[100001]; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + K = nextInt(); + bfs(); + System.out.println(minTime); + System.out.println(cnt); + } + + static void bfs() { + Queue q = new LinkedList<>(); + q.add(N); + visited[N] = 1; + while (!q.isEmpty()) { + int curr = q.poll(); + int time = visited[curr]; + if (curr == K) { + if (time-1 < minTime) { + minTime = time-1; + cnt = 1; + } else if (time-1 == minTime) cnt++; + continue; + } + for (int next : new int[] {curr-1, curr+1, curr*2}) { + if (next < 0 || next > 100000) continue; + + if (visited[next] == 0 || visited[next] == time+1) { + visited[next] = time+1; + q.add(next); + } + } + } + } +} +```