From 5b832a8af78a338deb238b5dc6f05b02720612a7 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:41:56 +0900 Subject: [PATCH] =?UTF-8?q?[20250826]=20BOJ=20/=20G5=20/=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../26 BOJ G5 \352\262\214\354\236\204.md" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "lkhyun/202508/26 BOJ G5 \352\262\214\354\236\204.md" diff --git "a/lkhyun/202508/26 BOJ G5 \352\262\214\354\236\204.md" "b/lkhyun/202508/26 BOJ G5 \352\262\214\354\236\204.md" new file mode 100644 index 00000000..8ac6ed46 --- /dev/null +++ "b/lkhyun/202508/26 BOJ G5 \352\262\214\354\236\204.md" @@ -0,0 +1,80 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static int[][] map = new int[501][501]; + static int[][] dist = new int[501][501]; + static int[] dx = {0, 0, -1, 1}; + static int[] dy = {1, -1, 0, 0}; + + public static void main(String[] args) throws IOException { + for (int[] row : dist) Arrays.fill(row, Integer.MAX_VALUE); + + int n = Integer.parseInt(br.readLine()); + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + int x1 = Integer.parseInt(st.nextToken()); + int y1 = Integer.parseInt(st.nextToken()); + int x2 = Integer.parseInt(st.nextToken()); + int y2 = Integer.parseInt(st.nextToken()); + + for (int x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) { + for (int y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) { + map[x][y] = 1; + } + } + } + + int m = Integer.parseInt(br.readLine()); + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int x1 = Integer.parseInt(st.nextToken()); + int y1 = Integer.parseInt(st.nextToken()); + int x2 = Integer.parseInt(st.nextToken()); + int y2 = Integer.parseInt(st.nextToken()); + + for (int x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) { + for (int y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) { + map[x][y] = 2; + } + } + } + + int result = BFS(); + bw.write(String.valueOf(result)); + bw.close(); + } + + private static int BFS() { + ArrayDeque q = new ArrayDeque<>(); + dist[0][0] = 0; + q.add(new int[]{0, 0}); + + while (!q.isEmpty()) { + int[] cur = q.poll(); + int x = cur[0], y = cur[1]; + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (nx < 0 || nx > 500 || ny < 0 || ny > 500 || map[nx][ny] == 2) continue; + + int newDist = dist[x][y] + map[nx][ny]; + if (newDist < dist[nx][ny]) { + dist[nx][ny] = newDist; + if (map[nx][ny] == 0) q.addFirst(new int[]{nx, ny}); + else q.addLast(new int[]{nx, ny}); + } + } + } + + return dist[500][500] == Integer.MAX_VALUE ? -1 : dist[500][500]; + } +} +```