|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.PriorityQueue; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +import org.w3c.dom.Node; |
| 10 | + |
| 11 | +public class Main { |
| 12 | + static final int MAX = 500; |
| 13 | + static final int INF = Integer.MAX_VALUE; |
| 14 | + static int[][] map = new int[MAX + 1][MAX + 1]; // 0: safe, 1: danger, 2: death |
| 15 | + static int[][] dist = new int[MAX + 1][MAX + 1]; |
| 16 | + static int[] dr = {1, -1, 0, 0}; |
| 17 | + static int[] dc = {0, 0, 1, -1}; |
| 18 | + |
| 19 | + static class Node { |
| 20 | + int r, c, cost; |
| 21 | + Node(int r, int c, int cost) { |
| 22 | + this.r = r; |
| 23 | + this.c = c; |
| 24 | + this.cost = cost; |
| 25 | + } |
| 26 | + } |
| 27 | + public static void main(String[] args) throws IOException { |
| 28 | + var br = new BufferedReader(new InputStreamReader(System.in)); |
| 29 | + StringTokenizer st; |
| 30 | + int N = Integer.parseInt(br.readLine()); |
| 31 | + for (int i = 0; i < N; i++) { |
| 32 | + st = new StringTokenizer(br.readLine()); |
| 33 | + int x1 = Integer.parseInt(st.nextToken()); |
| 34 | + int y1 = Integer.parseInt(st.nextToken()); |
| 35 | + int x2 = Integer.parseInt(st.nextToken()); |
| 36 | + int y2 = Integer.parseInt(st.nextToken()); |
| 37 | + markArea(x1, y1, x2, y2, 1); |
| 38 | + } |
| 39 | + int M = Integer.parseInt(br.readLine()); |
| 40 | + for (int i = 0; i < M; i++) { |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + int x1 = Integer.parseInt(st.nextToken()); |
| 43 | + int y1 = Integer.parseInt(st.nextToken()); |
| 44 | + int x2 = Integer.parseInt(st.nextToken()); |
| 45 | + int y2 = Integer.parseInt(st.nextToken()); |
| 46 | + markArea(x1, y1, x2, y2, 2); |
| 47 | + } |
| 48 | + |
| 49 | + for (int[] row : dist) |
| 50 | + Arrays.fill(row, INF); |
| 51 | + dist[0][0] = 0; |
| 52 | + PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> a.cost - b.cost); |
| 53 | + pq.add(new Node(0, 0, 0)); |
| 54 | + while (!pq.isEmpty()) { |
| 55 | + Node cur = pq.poll(); |
| 56 | + if (cur.cost > dist[cur.r][cur.c]) |
| 57 | + continue; |
| 58 | + if (cur.r == MAX && cur.c == MAX) |
| 59 | + break; |
| 60 | + for (int d = 0; d < 4; d++) { |
| 61 | + int nr = cur.r + dr[d]; |
| 62 | + int nc = cur.c + dc[d]; |
| 63 | + if (nr < 0 || nr > MAX || nc < 0 || nc > MAX) |
| 64 | + continue; |
| 65 | + if (map[nr][nc] == 2) |
| 66 | + continue; |
| 67 | + int ncst = cur.cost + (map[nr][nc] == 1 ? 1 : 0); |
| 68 | + if (ncst < dist[nr][nc]) { |
| 69 | + dist[nr][nc] = ncst; |
| 70 | + pq.add(new Node(nr, nc, ncst)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + int answer = dist[MAX][MAX]; |
| 75 | + System.out.println(answer == INF ? -1 : answer); |
| 76 | + } |
| 77 | + |
| 78 | + static void markArea(int x1, int y1, int x2, int y2, int val) { |
| 79 | + int sx = Math.min(x1, x2); |
| 80 | + int ex = Math.max(x1, x2); |
| 81 | + int sy = Math.min(y1, y2); |
| 82 | + int ey = Math.max(y1, y2); |
| 83 | + for (int i = sx; i <= ex; i++) { |
| 84 | + for (int j = sy; j <= ey; j++) { |
| 85 | + map[i][j] = val; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +``` |
0 commit comments