POJ1228 Grandpa's Estate

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1228

 思路:必须保证凸包的每条边上至少有3个原始点,如果给出的点小于等于5肯定是NO,因为最少的凸边形还需要6个点,最后要判断,如果凸包是一条线,那么还是NO。

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class Main {
  5.     class Point {
  6.         int x;
  7.         int y;
  8.         public Point(int x, int y) {
  9.             this.x = x;
  10.             this.y = y;
  11.         }
  12.     }
  13.     public static void main(String[] args) throws NumberFormatException,
  14.             IOException {
  15.         Main main = new Main();
  16.         BufferedReader read = new BufferedReader(new InputStreamReader(
  17.                 System.in));
  18.         int n = Integer.parseInt(read.readLine());
  19.         Point[] p;
  20.         Point[] ch;
  21.         int x, y;
  22.         String[] s;
  23.         int len;
  24.         int t;
  25.         boolean b;
  26.         for (int i = 0; i < n; i++) {
  27.             int m = Integer.parseInt(read.readLine());
  28.             p = new Point[m];
  29.             for (int j = 0; j < m; j++) {
  30.                 s = read.readLine().split(" ");
  31.                 x = Integer.parseInt(s[0]);
  32.                 y = Integer.parseInt(s[1]);
  33.                 p[j] = main.new Point(x, y);
  34.             }
  35.             if (m <= 5) {
  36.                 System.out.println("NO");
  37.             } else {
  38.                 ch = new Point[m];
  39.                 len = 0;
  40.                 len = Graham_scan(p, ch, m);
  41.                 b = false;
  42.                 t = 0;
  43.                 for (int j = 0; j < len - 2; j++) {
  44.                     if (!AtOneLine(ch[j], ch[j + 1], ch[j + 2])) {
  45.                         b = true;
  46.                         break;
  47.                     }
  48.                 }
  49.                 if (b) {
  50.                     for (int j = 0; j < len - 1; j++) {
  51.                         for (int k = 0; k < m; k++) {
  52.                             if (AtOneLine(ch[j], ch[j + 1], p[k])) {
  53.                                 t++;
  54.                             }
  55.                         }
  56.                         if (t < 3) {
  57.                             b = false;
  58.                             break;
  59.                         }
  60.                         t = 0;
  61.                     }
  62.                 }
  63.                 if (b) {
  64.                     System.out.println("YES");
  65.                 } else {
  66.                     System.out.println("NO");
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     public static boolean AtOneLine(Point p1, Point p2, Point p3) {
  72.         return ((p1.x - p2.x) * (p1.y - p3.y) == (p1.x - p3.x) * (p1.y - p2.y));
  73.     }
  74.     public static double multiply(Point p1, Point p2, Point p0) {
  75.         return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
  76.     }
  77.     public static double distance(Point p1, Point p2) {
  78.         return (Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
  79.                 * (p1.y - p2.y)));
  80.     }
  81.     public static int Graham_scan(Point[] PointSet, Point[] ch, int n) {
  82.         int i, j, k = 0, top = 2;
  83.         Point tmp;
  84.         for (i = 1; i < n; i++)
  85.             if ((PointSet[i].y < PointSet[k].y)
  86.                     || ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
  87.                 k = i;
  88.         tmp = PointSet[0];
  89.         PointSet[0] = PointSet[k];
  90.         PointSet[k] = tmp;
  91.         for (i = 1; i < n - 1; i++) {
  92.             k = i;
  93.             for (j = i + 1; j < n; j++)
  94.                 if ((multiply(PointSet[j], PointSet[k], PointSet[0]) > 0)
  95.                         || ((multiply(PointSet[j], PointSet[k], PointSet[0]) == 0) && (distance(
  96.                                 PointSet[0], PointSet[j]) < distance(
  97.                                 PointSet[0], PointSet[k]))))
  98.                     k = j;
  99.             tmp = PointSet[i];
  100.             PointSet[i] = PointSet[k];
  101.             PointSet[k] = tmp;
  102.         }
  103.         ch[0] = PointSet[0];
  104.         ch[1] = PointSet[1];
  105.         ch[2] = PointSet[2];
  106.         for (i = 3; i < n; i++) {
  107.             while (top > 0 && multiply(PointSet[i], ch[top], ch[top - 1]) >= 0)
  108.                 top--;
  109.             ch[++top] = PointSet[i];
  110.         }
  111.         return top + 1;
  112.     }
  113. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值