From 3685c17736c0dee4293edfd589a598ca38fca1ce Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 17 Jun 2025 20:13:22 +0900 Subject: [PATCH] =?UTF-8?q?[20250617]=20BOJ=20/=20G4=20/=20=EC=88=A8?= =?UTF-8?q?=EB=B0=94=EA=BC=AD=EC=A7=88=202=20/=20=EA=B9=80=EC=88=98?= =?UTF-8?q?=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\260\224\352\274\255\354\247\210 2.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "suyeun84/202506/17 BOJ G4 \354\210\250\353\260\224\352\274\255\354\247\210 2.md" 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); + } + } + } + } +} +```