import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static boolean isTouch(int[] line1_pts, int[] line2_pts) {
return Line2D.linesIntersect(line1_pts[0], line1_pts[1], line1_pts[2], line1_pts[3], line2_pts[0], line2_pts[1], line2_pts[2], line2_pts[3]);
}
public static int solve(int[][] points, int n) {
int lines_n = points.length;
ArrayList<ArrayList<Integer>> res = new ArrayList<>(lines_n);
for (int i = 0; i < lines_n; i++) {
res.add(new ArrayList<>());
}
for (int i = 0; i < lines_n; i++) {
for (int j = 0; j < lines_n; j++) {
if (j != i) {
if (isTouch(points[i], points[j])) {
res.get(i).add(j);
}
}
}
}
for (int i = 0; i < lines_n; i++) {
res.get(i).add(i);
}
// x1,y1,x2,y2 找出最小的y和最大的y
for (int i = 0; i < lines_n; i++) {
ArrayList<Integer> indexs = res.get(i);
int size = indexs.size();
int y_min = 500;
int y_max = 0;
for (int j = 0; j < size; j++) {
int max = Math.max(points[indexs.get(j)][1], points[indexs.get(j)][3]);
int min = Math.min(points[indexs.get(j)][1], points[indexs.get(j)][3]);
if (max > y_max) {
y_max = max;
}
if (min < y_min) {
y_min = min;
}
}
if (y_min <= 0 && y_max >= n) {
return 0;
}
}
return 1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int lines_n = sc.nextInt();
int[][] points = new int[lines_n][4];
for (int i = 0; i < lines_n; i++) {
for (int j = 0; j < 4; j++) {
points[i][j] = sc.nextInt();
}
}
System.out.println(solve(points, n));
}
}
}
阿里算法工程师实习生第一道大题(通过率60%)
最新推荐文章于 2021-02-22 01:54:47 发布