From 1f5a9dfc583b42dd76797aca8e134b49dc1268f6 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Mon, 10 Nov 2025 21:40:25 +0900 Subject: [PATCH] =?UTF-8?q?[20251110]=20BOJ=20/=20G4=20/=20=EC=A0=90?= =?UTF-8?q?=ED=94=84=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10 BOJ \354\240\220\355\224\204.md" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "0224LJH/202511/10 BOJ \354\240\220\355\224\204.md" diff --git "a/0224LJH/202511/10 BOJ \354\240\220\355\224\204.md" "b/0224LJH/202511/10 BOJ \354\240\220\355\224\204.md" new file mode 100644 index 00000000..3140d84f --- /dev/null +++ "b/0224LJH/202511/10 BOJ \354\240\220\355\224\204.md" @@ -0,0 +1,84 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + + static int[][] dp = new int[10001][500]; + static HashSet set = new HashSet<>(); + static int total,setSize,ans; + + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + + + } + + private static void init() throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + total = Integer.parseInt(st.nextToken()); + setSize = Integer.parseInt(st.nextToken()); + for (int i = 0; i < setSize; i++) { + set.add(Integer.parseInt(br.readLine())); + } + for (int i = 0; i <= 10000; i++) { + Arrays.fill(dp[i], Integer.MAX_VALUE); + } + + + } + + private static void process() throws IOException { + if (set.contains(2)) { + ans = -1; + return; + } + dp[2][1] = 1; + for (int i = 2; i< total; i++) { + if (set.contains(i)) continue; + for (int j = 1; j < 500; j++) { + if (dp[i][j] == Integer.MAX_VALUE) continue; + + int more = j+1; + int less =j-1; + if (more < 500 && i+more <= total && !set.contains(i+more)) { + dp[i+more][more] = Math.min(dp[i+more][more], dp[i][j]+1); + } + if (j < 500 && i+j <= total && !set.contains(i+j)) { + dp[i+j][j] = Math.min(dp[i+j][j], dp[i][j]+1); + } + if (less != 0 && i+less <=total && !set.contains(i+less)) { + dp[i+less][less] = Math.min(dp[i+less][less], dp[i][j]+1); + } + } + } + + ans = Integer.MAX_VALUE; + for (int i = 1; i < 500; i++) { + ans = Math.min(ans, dp[total][i]); + } + if (ans == Integer.MAX_VALUE) ans = -1; + + } + + + private static void print() { + System.out.print(ans); + } + +} +```