From d25cbf325d5fc95ea2fb5ba0d97ead08639babb9 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Mon, 10 Nov 2025 23:58:02 +0900 Subject: [PATCH] =?UTF-8?q?[20251110]=20BOJ=20/=20G4=20/=20=EC=A0=90?= =?UTF-8?q?=ED=94=84=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10 BOJ G4 \354\240\220\355\224\204.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "Seol-JY/202511/10 BOJ G4 \354\240\220\355\224\204.md" diff --git "a/Seol-JY/202511/10 BOJ G4 \354\240\220\355\224\204.md" "b/Seol-JY/202511/10 BOJ G4 \354\240\220\355\224\204.md" new file mode 100644 index 00000000..c9d5fa36 --- /dev/null +++ "b/Seol-JY/202511/10 BOJ G4 \354\240\220\355\224\204.md" @@ -0,0 +1,63 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + boolean[] canStep = new boolean[N + 1]; + Arrays.fill(canStep, true); + + for (int i = 0; i < M; i++) { + int small = Integer.parseInt(br.readLine()); + canStep[small] = false; + } + + int maxSpeed = 200; + int[][] dist = new int[N + 1][maxSpeed + 1]; + for (int i = 0; i <= N; i++) { + Arrays.fill(dist[i], Integer.MAX_VALUE); + } + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[2] - b[2]); + pq.offer(new int[]{1, 0, 0}); + dist[1][0] = 0; + + while (!pq.isEmpty()) { + int[] cur = pq.poll(); + int pos = cur[0]; + int speed = cur[1]; + int jumps = cur[2]; + + if (pos == N) { + System.out.println(jumps); + return; + } + + if (dist[pos][speed] < jumps) { + continue; + } + + for (int nextSpeed = Math.max(1, speed - 1); nextSpeed <= speed + 1; nextSpeed++) { + int nextPos = pos + nextSpeed; + + if (nextPos > N || nextPos < 1 || !canStep[nextPos] || nextSpeed > maxSpeed) { + continue; + } + + if (dist[nextPos][nextSpeed] > jumps + 1) { + dist[nextPos][nextSpeed] = jumps + 1; + pq.offer(new int[]{nextPos, nextSpeed, jumps + 1}); + } + } + } + + System.out.println(-1); + } +} +```