From 3d3f9846652994800ada59fb791c38b07ba8e57d Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 28 Aug 2025 09:04:19 +0900 Subject: [PATCH] =?UTF-8?q?[20250828]=20BOJ=20/=20G4=20/=20=EC=9D=B4?= =?UTF-8?q?=EB=AA=A8=ED=8B=B0=EC=BD=98=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\353\252\250\355\213\260\354\275\230.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "JHLEE325/202508/28 BOJ G4 \354\235\264\353\252\250\355\213\260\354\275\230.md" diff --git "a/JHLEE325/202508/28 BOJ G4 \354\235\264\353\252\250\355\213\260\354\275\230.md" "b/JHLEE325/202508/28 BOJ G4 \354\235\264\353\252\250\355\213\260\354\275\230.md" new file mode 100644 index 00000000..5b349e04 --- /dev/null +++ "b/JHLEE325/202508/28 BOJ G4 \354\235\264\353\252\250\355\213\260\354\275\230.md" @@ -0,0 +1,61 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static class State { + int cur, clip; + + State(int cur, int clip) { + this.cur = cur; + this.clip = clip; + } + } + + static int s; + static int range = 2000; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + s = Integer.parseInt(br.readLine()); + System.out.println(bfs()); + } + + static int bfs() { + int[][] dist = new int[range + 1][range + 1]; + for (int i = 0; i <= range; i++) Arrays.fill(dist[i], -1); + + ArrayDeque q = new ArrayDeque<>(); + q.add(new State(1, 0)); + dist[1][0] = 0; + + while (!q.isEmpty()) { + State now = q.poll(); + int cur = now.cur; + int clip = now.clip; + int t = dist[cur][clip]; + + if (cur == s) + return t; + + if (dist[cur][cur] == -1) { + dist[cur][cur] = t + 1; + q.add(new State(cur, cur)); + } + + if (clip > 0 && cur + clip <= range && dist[cur + clip][clip] == -1) { + dist[cur + clip][clip] = t + 1; + q.add(new State(cur + clip, clip)); + } + + if (cur > 0 && dist[cur - 1][clip] == -1) { + dist[cur - 1][clip] = t + 1; + q.add(new State(cur - 1, clip)); + } + } + return -1; + } +} + +```