首页 > 专题系列 > Java解POJ > POJ 1228 Grandpa’s Estate [解题报告] Java
2013
11-09

POJ 1228 Grandpa’s Estate [解题报告] Java

Grandpa’s Estate

问题描述 :

Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa’s belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa’s birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.

输入:

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.

输出:

There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.

样例输入:

1
6 
0 0
1 2
3 4
2 0
2 4 
5 0

样例输出:

NO

解题代码:

import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;

 public class Main {

     class Point {
         int x;
         int y;

         public Point(int x, int y) {
             this.x = x;
             this.y = y;
         }
     }

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         Main main = new Main();
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int n = Integer.parseInt(read.readLine());
         Point[] p;
         Point[] ch;
         int x, y;
         String[] s;
         int len;
         int t;
         boolean b;
         for (int i = 0; i < n; i++) {
             int m = Integer.parseInt(read.readLine());
             p = new Point[m];
             for (int j = 0; j < m; j++) {
                 s = read.readLine().split(" ");
                 x = Integer.parseInt(s[0]);
                 y = Integer.parseInt(s[1]);
                 p[j] = main.new Point(x, y);
             }
             if (m <= 5) {
                 System.out.println("NO");
             } else {
                 ch = new Point[m];
                 len = 0;
                 len = Graham_scan(p, ch, m);
                 b = false;
                 t = 0;
                 for (int j = 0; j < len - 2; j++) {
                     if (!AtOneLine(ch[j], ch[j + 1], ch[j + 2])) {
                         b = true;
                         break;
                     }
                 }
                 if (b) {
                     for (int j = 0; j < len - 1; j++) {
                         for (int k = 0; k < m; k++) {
                             if (AtOneLine(ch[j], ch[j + 1], p[k])) {
                                 t++;
                             }
                         }
                         if (t < 3) {
                             b = false;
                             break;
                         }
                         t = 0;
                     }
                 }
                 if (b) {
                     System.out.println("YES");
                 } else {
                     System.out.println("NO");
                 }
             }
         }
     }

     public static boolean AtOneLine(Point p1, Point p2, Point p3) {
         return ((p1.x - p2.x) * (p1.y - p3.y) == (p1.x - p3.x) * (p1.y - p2.y));
     }

     public static double multiply(Point p1, Point p2, Point p0) {
         return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));

     }

     public static double distance(Point p1, Point p2) {
         return (Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
                 * (p1.y - p2.y)));
     }

     public static int Graham_scan(Point[] PointSet, Point[] ch, int n) {
         int i, j, k = 0, top = 2;
         Point tmp;
         for (i = 1; i < n; i++)
             if ((PointSet[i].y < PointSet[k].y)
                     || ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
                 k = i;
         tmp = PointSet[0];
         PointSet[0] = PointSet[k];
         PointSet[k] = tmp;
         for (i = 1; i < n - 1; i++) {
             k = i;
             for (j = i + 1; j < n; j++)
                 if ((multiply(PointSet[j], PointSet[k], PointSet[0]) > 0)
                         || ((multiply(PointSet[j], PointSet[k], PointSet[0]) == 0) && (distance(
                                 PointSet[0], PointSet[j]) < distance(
                                 PointSet[0], PointSet[k]))))
                     k = j;
             tmp = PointSet[i];
             PointSet[i] = PointSet[k];
             PointSet[k] = tmp;
         }
         ch[0] = PointSet[0];
         ch[1] = PointSet[1];
         ch[2] = PointSet[2];
         for (i = 3; i < n; i++) {
             while (top > 0 && multiply(PointSet[i], ch[top], ch[top - 1]) >= 0)
                 top--;
             ch[++top] = PointSet[i];
         }
         return top + 1;
     }
}