标签:
Grandpa‘s Estate
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11289 Accepted: 3117 Description
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.Input
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.Output
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.Sample Input
1 6 0 0 1 2 3 4 2 0 2 4 5 0Sample Output
NOSource
/************************************************************************* > File Name: poj_1228.cpp > Author: Howe_Young > Mail: 1013410795@qq.com > Created Time: 2015年05月07日 星期四 16时44分36秒 ************************************************************************/ #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #define EPS 1e-8 using namespace std; const int maxn = 1004; struct point{ double x, y; }; point p[maxn], convex[maxn]; double Min(double a, double b) { return a < b ? a : b; } double Max(double a, double b) { return a > b ? a : b; } int sgn(double x) { if (fabs(x) < EPS) return 0; return x < 0 ? -1 : 1; } double x_multi(point p1, point p2, point p3) { return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y); } bool cmp(const point p1, const point p2) { return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y); } void convex_hull(point *p, point *convex, int n, int &len) { sort(p, p + n, cmp); int top = 1; convex[0] = p[0]; convex[1] = p[1]; //找出凸包的下半部分凸壳 for (int i = 2; i < n; i++) { while (top > 0 && sgn(x_multi(convex[top - 1], convex[top], p[i])) <= 0)//大于0为逆时针,小于0为顺时针 top--; convex[++top] = p[i]; } int tmp = top; //找出凸包的上半部分,因为我的比较函数是写的y优先的,所以上下部分,当然也可以以x优先排序,这时候就是左右部分了 for (int i = n - 2; i >= 0; i--) { while (top > tmp && sgn(x_multi(convex[top - 1], convex[top], p[i])) <= 0)//大于0为逆时针,小于0为顺时针 top--; convex[++top] = p[i];//存放凸包中的点 } len = top; } bool on_segment(point p1, point p2, point p3)//判断p3是否在线段p1p2上 { double minx, miny, maxx, maxy; minx = Min(p1.x, p2.x); maxx = Max(p1.x, p2.x); miny = Min(p1.y, p2.y); maxy = Max(p1.y, p2.y); return (sgn(x_multi(p1, p2, p3)) == 0 && (sgn(p3.x - minx) >= 0 && sgn(p3.x - maxx) <= 0 && sgn(p3.y - miny) >= 0 && sgn(p3.y - maxy) <= 0)); } bool check(point *p, point p1, point p2, int n) { int cnt = 0; for (int i = 0; i < n; i++) { if (on_segment(p1, p2, p[i])) cnt++; } if (cnt == n)//特判,如果给定的点成一条线时,不符合 return false; return cnt >= 3; } int main() { int kase, n; scanf("%d", &kase); while (kase--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y); int len; if (n <= 5)//n <= 5之前的点都是不确定的 { puts("NO"); continue; } convex_hull(p, convex, n, len); convex[len] = convex[0]; bool flag = false; for (int i = 0; i < len; i++)//检查凸包中的每一个边 { if (!check(p, convex[i], convex[i + 1], n)) { flag = true; break; } } if (!flag) puts("YES"); else puts("NO"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Howe-Young/p/4485903.html