您的位置:首页 > 其它

poj 1228 Grandpa's Estate

2016-08-27 17:05 239 查看
题意:判断给出的点组成的凸包是不是稳定凸包

稳定土包:凸包的每条边上至少有三个点

题意略难理解..............

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
struct node
{
double x,y;
}p[MAXN];
int res[MAXN],top,n;
bool vis[MAXN];

bool cmp(const node &a, const node &b)
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
}

double multi(node &a, node &b, node &c)
{
return (a.x - c.x)*(b.y - c.y) - (b.x - c.x)*(a.y - c.y);
}

void Graham()
{
int len;
top = 1;

sort(p,p+n,cmp);
for(int i = 0; i < 3; i++)
res[i] = i;
for (int i = 2; i < n; ++i)
{
while(top && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)
top--;
res[++top] = i;
}
len = top;
res[++top] = n-2;
for(int i = n-3; i >= 0; i--)
{
while(top != len && multi(p[i], p[res[top]], p[res[top-1]]) >= 0)//res只保留了凸包的各个顶点
top--;
res[++top] = i;
}
}

int main(int argc, char const *argv[])
{
int t,flag;

scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);

if(n < 6)//n=6时,3个点为顶点,3个点在边上
printf("NO\n");
else
{
Graham();
for(int i = 0; i <= n; i++)
vis[i] = false;//初始化
for(int i = 0; i < top; i++)
vis[res[i]] = true;//不是土包顶点的vis值为false

for(int i = 0; i < top; i++)//遍历所有的边
{
flag = 0;
for(int j = 0; j < n; j++)//遍历所有点,是否有点是在边上的,若没有就输出NO
{
if(!vis[j])
{
if(multi(p[res[i+1]], p[j], p[res[i]]) == 0)
{
flag = 1;
vis[j] = true;
}
}
}//找过所有的点,判断
if(!flag)
{
printf("NO\n");
break;
}
}
if(flag)//所有的边除了端点,还有点
printf("YES\n");
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: