From b75055eac1ddcfc2546b2c720a8090d51ebe04b5 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 30 Oct 2025 23:20:13 +0900 Subject: [PATCH] =?UTF-8?q?[20251030]=20BOJ=20/=20G3=20/=20=EB=A7=90?= =?UTF-8?q?=EC=9D=B4=20=EB=90=98=EA=B3=A0=ED=94=88=20=EC=9B=90=EC=88=AD?= =?UTF-8?q?=EC=9D=B4=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \354\233\220\354\210\255\354\235\264.md" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "suyeun84/202510/30 BOJ G3 \353\247\220\354\235\264 \353\220\230\352\263\240\355\224\210 \354\233\220\354\210\255\354\235\264.md" diff --git "a/suyeun84/202510/30 BOJ G3 \353\247\220\354\235\264 \353\220\230\352\263\240\355\224\210 \354\233\220\354\210\255\354\235\264.md" "b/suyeun84/202510/30 BOJ G3 \353\247\220\354\235\264 \353\220\230\352\263\240\355\224\210 \354\233\220\354\210\255\354\235\264.md" new file mode 100644 index 00000000..4bfff16c --- /dev/null +++ "b/suyeun84/202510/30 BOJ G3 \353\247\220\354\235\264 \353\220\230\352\263\240\355\224\210 \354\233\220\354\210\255\354\235\264.md" @@ -0,0 +1,80 @@ +```java +import java.io.*; +import java.util.*; + +public class boj1600 { + 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[][] dir = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; + static int[][] hdir = new int[][] {{-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}}; + static int K, W, H; + static int[][] board; + static boolean[][][] visited; + public static void main(String[] args) throws Exception { + nextLine(); + K = nextInt(); + nextLine(); + W = nextInt(); + H = nextInt(); + board = new int[H][W]; + visited = new boolean[H][W][K+1]; + for (int i = 0; i < H; i++) { + nextLine(); + for (int j = 0; j < W; j++) { + board[i][j] = nextInt(); + } + } + bfs(); + } + + static void bfs() { + Queue q = new LinkedList<>(); + q.add(new Move(0, 0, 0, 0)); + visited[0][0][0] = true; + + while (!q.isEmpty()) { + Move cur = q.poll(); + if (cur.y == H-1 && cur.x == W-1) { + System.out.println(cur.dep); + return; + } + + for (int[] d : dir) { + int ny = cur.y + d[0]; + int nx = cur.x + d[1]; + if (ny >= H || ny < 0 || nx >= W || nx < 0 || board[ny][nx] == 1) continue; + + if (!visited[ny][nx][cur.horse]) { + visited[ny][nx][cur.horse] = true; + q.offer(new Move(ny, nx, cur.horse, cur.dep + 1)); + } + } + if (cur.horse < K) { + for (int[] d : hdir) { + int ny = cur.y + d[0]; + int nx = cur.x + d[1]; + if (ny >= H || ny < 0 || nx >= W || nx < 0 || board[ny][nx] == 1) continue; + if (!visited[ny][nx][cur.horse+1]) { + visited[ny][nx][cur.horse+1] = true; + q.offer(new Move(ny, nx, cur.horse+1, cur.dep + 1)); + } + } + } + } + System.out.println(-1); + } + + static class Move { + int y, x, horse, dep; + public Move(int y, int x, int horse, int dep) { + this.y = y; + this.x = x; + this.horse = horse; + this.dep = dep; + } + } +} +```