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 0

Sample Output

NO

Source

题目大意: 给定n个点, 都是凸包上的顶点, 问这个凸包是否为稳定凸包,就是唯一确定一个凸包
思路: 刚开始老想着怎么判定它是否是凸包就完事了, 交了好几次都是wa, 后来去网上一看, 原来不是这样的。。。是求稳定凸包的,如果给定的顶点唯一确定的一个凸包的话, 那么这个多边形的每一条边上的顶点个数一定是大于等于三个的(包括端点), 只有这样才能保证是稳定的。如图所示。
图一中, 如果AB之间没有第三个点的话, 那么就有可能出现一个点E还使得原图形为凸包, 所以它是不稳定的, 图二就是稳定的了,有了这些点,他可以唯一确定一个凸包。知道了这个之后写代码就容易多了。
我的代码:
/*************************************************************************
> 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 = ;
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 ;
return x < ? - : ;
}
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 = ;
convex[] = p[];
convex[] = p[];
//找出凸包的下半部分凸壳
for (int i = ; i < n; i++)
{
while (top > && sgn(x_multi(convex[top - ], convex[top], p[i])) <= )//大于0为逆时针,小于0为顺时针
top--;
convex[++top] = p[i];
}
int tmp = top;
//找出凸包的上半部分,因为我的比较函数是写的y优先的,所以上下部分,当然也可以以x优先排序,这时候就是左右部分了
for (int i = n - ; i >= ; i--)
{
while (top > tmp && sgn(x_multi(convex[top - ], convex[top], p[i])) <= )//大于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)) == && (sgn(p3.x - minx) >= && sgn(p3.x - maxx) <= && sgn(p3.y - miny) >= && sgn(p3.y - maxy) <= ));
}
bool check(point *p, point p1, point p2, int n)
{
int cnt = ;
for (int i = ; i < n; i++)
{
if (on_segment(p1, p2, p[i]))
cnt++;
}
if (cnt == n)//特判,如果给定的点成一条线时,不符合
return false;
return cnt >= ;
}
int main()
{
int kase, n;
scanf("%d", &kase);
while (kase--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
int len;
if (n <= )//n <= 5之前的点都是不确定的
{
puts("NO");
continue;
}
convex_hull(p, convex, n, len);
convex[len] = convex[];
bool flag = false;
for (int i = ; i < len; i++)//检查凸包中的每一个边
{
if (!check(p, convex[i], convex[i + ], n))
{
flag = true;
break;
}
}
if (!flag)
puts("YES");
else
puts("NO");
}
return ;
}

POJ 1228 Grandpa's Estate(凸包)的更多相关文章

  1. POJ 1228 Grandpa's Estate 凸包 唯一性

    LINK 题意:给出一个点集,问能否够构成一个稳定凸包,即加入新点后仍然不变. 思路:对凸包的唯一性判断,对任意边判断是否存在三点及三点以上共线,如果有边不满足条件则NO,注意使用水平序,这样一来共线 ...

  2. POJ 1228 - Grandpa's Estate 稳定凸包

    稳定凸包问题 要求每条边上至少有三个点,且对凸包上点数为1,2时要特判 巨坑无比,调了很长时间= = //POJ 1228 //稳定凸包问题,等价于每条边上至少有三个点,但对m = 1(点)和m = ...

  3. POJ 1228 Grandpa's Estate(凸包唯一性判断)

    Description Being the only living descendant of his grandfather, Kamran the Believer inherited all o ...

  4. POJ 1228 Grandpa's Estate --深入理解凸包

    题意: 判断凸包是否稳定. 解法: 稳定凸包每条边上至少有三个点. 这题就在于求凸包的细节了,求凸包有两种算法: 1.基于水平序的Andrew算法 2.基于极角序的Graham算法 两种算法都有一个类 ...

  5. 简单几何(求凸包点数) POJ 1228 Grandpa's Estate

    题目传送门 题意:判断一些点的凸包能否唯一确定 分析:如果凸包边上没有其他点,那么边想象成橡皮筋,可以往外拖动,这不是唯一确定的.还有求凸包的点数<=2的情况一定不能确定. /********* ...

  6. poj - 1228 - Grandpa's Estate

    题意:原来一个凸多边形删去一些点后剩n个点,问这个n个点能否确定原来的凸包(1 <= 测试组数t <= 10,1 <= n <= 1000). 题目链接:http://poj. ...

  7. 【POJ】1228 Grandpa's Estate(凸包)

    http://poj.org/problem?id=1228 随便看看就能发现,凸包上的每条边必须满足,有相邻的边和它斜率相同(即共线或凸包上每个点必须一定在三点共线上) 然后愉快敲完凸包+斜率判定, ...

  8. 【POJ 1228】Grandpa's Estate 凸包

    找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include ...

  9. poj 1228 稳定凸包

    Grandpa's Estate Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12337   Accepted: 3451 ...

随机推荐

  1. 关于Redis的知识汇总[转]

    1. Overview 1.1 资料 <The Little Redis Book> ,最好的入门小册子,可以先于一切文档之前看,免费. 作者Antirez的博客,Antirez维护的Re ...

  2. 为什么Android AsyncTask的使用要遵循五大原则

    引言 AsyncTask是一个围绕Handler和Thread而设计的助手类,封装了在工作线程中与UI交互的细节,只需要对应重写几个回调方法即可,并使得代码更加简洁,优雅.但要注意的是AsyncTas ...

  3. __attribute__ ((section(".text")))的测试

    一.测试原因 在学习u-boot的环境变量过程中,看到有如此的代码,现对涉及到的内容进行实验测试. 二.测试目的 1.了解gcc允许对段的属性进行更改的方法. 2.解决”ENV_IS_EMBEDDED ...

  4. 单个ViewController支持横屏,其他全竖屏方法-b

    如果项目中用了navigationViewController, 那么就应该新建一个uinavigationViewController的子类,然后在这个类里面写上下面的代码,在使用的时候就用自定义的 ...

  5. linux中用户信息及密码相关知识

    在linux中若修改用户信息.密码,组群信息.密码等.其实是在修改/etc/passwd,/etc/shadow,/etc/group,/etc/groupshadow等文件的内容. 这四个文件的意思 ...

  6. 设计模式——如何避免在OO设计中违反依赖倒置原则

    1 变量不可以包含具体类的引用.一旦new,就对具体类产生依赖,用工厂模式来避开. 2 类不要派生至具体类.用派生抽象类避开. 3 不要覆盖基类已经实现的方法.基类中已实现的方法应该由所有子类共享.

  7. SVN:分支合并到主干

    合并日志: --- Merging r173674 through r175986 into '.': C src/test/java/com/test/rigel/sandbox/organizat ...

  8. swiper 多个循环的实现

    swiper 最好要一一对应,最好与id关联. new Swiper('#guess .swiper-container', { pagination: '#guess .swiper-paginat ...

  9. Vi的几种退出方式

    1.q 退出 2.w 保存,继续操作 3.wq 保存退出 4.q! 不保存,放弃修改 5.x 同wq相似,但又有区别 wq   强制性写入文件并退出.即使文件没有被修改也强制写入,并更新文件的修改时间 ...

  10. Sumdiv(各种数学)

    http://poj.org/problem?id=1845 题意:求A^B的所有约数的和再对9901取模: 做了这个学到了N多数学知识: 一:任意一个整数都可以唯一分解成素因子的乘积:A = p1^ ...