package practice; import java.util.Scanner; public class TreasureHunt { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int[][] walls; float x, y; int doors = Integer.MAX_VALUE, temp1, temp2; int n = cin.nextInt(); walls = new int[n][4]; for (int j = 0; j < n; j++) { walls[j][0] = cin.nextInt(); walls[j][1] = cin.nextInt(); walls[j][2] = cin.nextInt(); walls[j][3] = cin.nextInt(); } x = cin.nextFloat(); y = cin.nextFloat(); for (int j = 0; j < n; j++) { temp1 = find(walls[j][0], walls[j][1], x, y, walls, j); temp2 = find(walls[j][2], walls[j][3], x, y, walls, j); doors = temp1 < temp2 ? (temp1 < doors ? temp1 : doors) : (temp2 < doors ? temp2 : doors); } if (n == 0) doors = 0; doors++; System.out.println("Number of doors = " + doors); } private static int find(int x1, int y2, float x, float y, int[][] walls, int j) { int count = 0; for (int i = 0, len = walls.length; i < len; i++) { if (i == j) continue; if (isIntersect(x1, y2, x, y, walls[i])) count++; } return count; } /** * * 跨立实验 */ private static boolean isIntersect(int startX, int startY, float endX, float endY, int[] wall) { if ((Math.max(startX, endX) >= Math.min(wall[0], wall[2])) && (Math.max(wall[0], wall[2]) >= Math.min(startX, endX)) && (Math.max(startY, endY) >= Math.min(wall[1], wall[3])) && (Math.max(wall[1], wall[3]) >= Math.min(startY, endY)) && (multiply(wall[0], wall[1], endX, endY, startX, startY) * multiply(endX, endY, wall[2], wall[3], startX, startY) > 0) && (multiply(startX, startY, wall[2], wall[3], wall[0], wall[1]) * multiply(wall[2], wall[3], endX, endY, wall[0], wall[1]) > 0)) return true; else return false; } private static double multiply(float x1, float y1, float x2, float y2, float x3, float y3) { return ((x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)); } }
相关文章: