|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Bus{ |
| 7 | + int x1, y1, x2, y2; |
| 8 | + Bus(int x1, int y1, int x2, int y2){ |
| 9 | + this.x1 = x1; |
| 10 | + this.y1 = y1; |
| 11 | + this.x2 = x2; |
| 12 | + this.y2 = y2; |
| 13 | + } |
| 14 | + |
| 15 | + boolean intersect(Bus other) { |
| 16 | + |
| 17 | + if(this.x1 == this.x2 && other.x1 == other.x2) { |
| 18 | + if(this.x1 != other.x1) return false; |
| 19 | + if(this.y1 > other.y2 || this.y2 < other.y1) return false; |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + if(this.y1 == this.y2 && other.y1 == other.y2) { |
| 24 | + if(this.y1 != other.y1) return false; |
| 25 | + if(this.x1 > other.x2 || this.x2 < other.x1) return false; |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + if(this.x1 == this.x2) { |
| 30 | + if(other.x1 <= this.x1 && this.x1 <= other.x2 && this.y1 <= other.y1 && other.y1 <= this.y2) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + return false; |
| 35 | + } |
| 36 | + if(other.y1 <= this.y1 && this.y1 <= other.y2 && this.x1 <= other.x1 && other.x1 <= this.x2) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + return false; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class Main { |
| 45 | + |
| 46 | + // IO field |
| 47 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 48 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 49 | + static StringTokenizer st = new StringTokenizer(""); |
| 50 | + |
| 51 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 52 | + static String nextToken() throws Exception { |
| 53 | + if(!st.hasMoreTokens()) nextLine(); |
| 54 | + return st.nextToken(); |
| 55 | + } |
| 56 | + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } |
| 57 | + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } |
| 58 | + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } |
| 59 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 60 | + |
| 61 | + // Additional field |
| 62 | + |
| 63 | + static int M, N, K, sx, sy, ex, ey; |
| 64 | + static Bus[] bs; |
| 65 | + static boolean[] starts, ends; |
| 66 | + |
| 67 | + static boolean[][] V; |
| 68 | + |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + |
| 71 | + ready(); |
| 72 | + solve(); |
| 73 | + |
| 74 | + bwEnd(); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + static void ready() throws Exception{ |
| 79 | + |
| 80 | + M = nextInt(); |
| 81 | + N = nextInt(); |
| 82 | + K = nextInt(); |
| 83 | + bs = new Bus[K]; |
| 84 | + V = new boolean[K][K]; |
| 85 | + for(int i=0;i<K;i++) { |
| 86 | + int id = nextInt(), x1 = nextInt(), y1 = nextInt(), x2 = nextInt(), y2 = nextInt(); |
| 87 | + if(x1 == x2) { |
| 88 | + int min = Math.min(y1, y2); |
| 89 | + int max = Math.max(y1, y2); |
| 90 | + y1 = min; |
| 91 | + y2 = max; |
| 92 | + } |
| 93 | + else { |
| 94 | + int min = Math.min(x1, x2); |
| 95 | + int max = Math.max(x1, x2); |
| 96 | + x1 = min; |
| 97 | + x2 = max; |
| 98 | + } |
| 99 | + bs[i] = new Bus(x1,y1,x2,y2); |
| 100 | + } |
| 101 | + sx = nextInt(); |
| 102 | + sy = nextInt(); |
| 103 | + ex = nextInt(); |
| 104 | + ey = nextInt(); |
| 105 | + starts = new boolean[K]; |
| 106 | + ends = new boolean[K]; |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + static void solve() throws Exception{ |
| 111 | + |
| 112 | + for(int i=0;i<K;i++) { |
| 113 | + for(int j=i+1;j<K;j++) { |
| 114 | + if(bs[i].intersect(bs[j])) { |
| 115 | + V[i][j] = true; |
| 116 | + V[j][i] = true; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + for(int i=0;i<K;i++) { |
| 122 | + if(bs[i].intersect(new Bus(sx,sy,sx,sy))) starts[i] = true; |
| 123 | + if(bs[i].intersect(new Bus(ex,ey,ex,ey))) ends[i] = true; |
| 124 | + } |
| 125 | + |
| 126 | + Queue<int[]> Q = new LinkedList<>(); |
| 127 | + boolean[] vis = new boolean[K]; |
| 128 | + for(int i=0;i<K;i++) if(starts[i]) { |
| 129 | + vis[i] = true; |
| 130 | + Q.offer(new int[] {i,1}); |
| 131 | + } |
| 132 | + while(!Q.isEmpty()) { |
| 133 | + int[] now = Q.poll(); |
| 134 | + int n = now[0], t = now[1]; |
| 135 | + if(ends[n]) { |
| 136 | + bw.write(t + "\n"); |
| 137 | + return; |
| 138 | + } |
| 139 | + for(int i=0;i<K;i++) if(i!=n && V[n][i] && !vis[i]) { |
| 140 | + vis[i] = true; |
| 141 | + Q.offer(new int[] {i,t+1}); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +``` |
0 commit comments