From 0a520d84575cc831b1d256738af5b8401486ff58 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Wed, 19 Feb 2025 16:38:55 +0900 Subject: [PATCH] =?UTF-8?q?[20250219]=20BOJ=20/=20=EA=B3=A8=EB=93=9C5=20/?= =?UTF-8?q?=20=EC=9E=A5=EA=B5=B0=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 --- .../19 BOJ G5 \354\236\245\352\265\260.md" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "suyeun84/202502/19 BOJ G5 \354\236\245\352\265\260.md" diff --git "a/suyeun84/202502/19 BOJ G5 \354\236\245\352\265\260.md" "b/suyeun84/202502/19 BOJ G5 \354\236\245\352\265\260.md" new file mode 100644 index 00000000..5a19eaa9 --- /dev/null +++ "b/suyeun84/202502/19 BOJ G5 \354\236\245\352\265\260.md" @@ -0,0 +1,69 @@ +```java +import java.util.*; +import java.io.*; + +public class Main { + static Position king; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int[][] dir = new int[][] {{-3,-2},{-3,2},{3,-2},{3,2},{2,-3},{-2,-3},{-2,3},{2,3}}; + Position sang; + boolean[][] visited = new boolean[10][9]; + + sang = new Position(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), 0); + st = new StringTokenizer(br.readLine()); + king = new Position(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), 0); + + Queue q = new LinkedList<>(); + q.add(sang); + visited[sang.y][sang.x] = true; + while(!q.isEmpty()) { + Position curr = q.poll(); + for (int[] d : dir) { + int ny = curr.y + d[0]; + int nx = curr.x + d[1]; + if (ny >= 10 || ny < 0 || nx >= 9 || nx < 0 || visited[ny][nx]) continue; + if (check(d, curr)) continue; + if (ny == king.y && nx == king.x) { + System.out.println(curr.m+1); + return; + } + q.add(new Position(ny, nx, curr.m+1)); + visited[ny][nx] = true; + } + } + System.out.println(-1); + } + static boolean check(int[] d, Position curr) { + if (Math.abs(d[0]) == 2) { + int ny = curr.y; + int nx = curr.x + d[1]/3; + if(ny == king.y && nx == king.x) return true; + ny += d[0]/2; + nx += d[1]/3; + if(ny == king.y && nx == king.x) return true; + } else { + int ny = curr.y + d[0]/3; + int nx = curr.x; + if(ny == king.y && nx == king.x) return true; + ny += d[0]/3; + nx += d[1]/2; + if(ny == king.y && nx == king.x) return true; + } + return false; + } + + static class Position { + int y; + int x; + int m; + public Position(int y, int x, int m) { + this.y = y; + this.x = x; + this.m = m; + } + } +} +```