您的位置:首页 > 其它

poj1228 Grandpa's Estate(凸包)

2016-09-27 14:30 387 查看

poj1228

题目

给你一些肯定在凸包上的点,问这些点构成的凸包是不是稳定的凸包。

思路

稳定凸包的话每条边上至少三个给定点。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#define pi acos (-1)
#define rotate Rotate

using namespace std;

typedef long long ll;

const double eps = 1e-8;
const int maxn=1010;
const double INF = 1e20;

int dcmp (double x)
{
if (f
4000
abs (x) < eps)
return 0;
else return x < 0 ? -1 : 1;
}

struct point
{
double x, y;
int pos;
point (double _x = 0, double _y = 0,int _pos=0) : x(_x), y(_y),pos(_pos) {}
point operator - (point a) const
{
return point (x-a.x, y-a.y);
}
point operator + (point a) const
{
return point (x+a.x, y+a.y);
}
bool operator < (const point &a) const
{
return x < a.x || (x == a.x && y < a.y);
}
bool operator == (const point &a) const
{
return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0;
}
};

point operator * (point a, double p)
{
return point (a.x*p, a.y*p);
}
double cross (point a, point b)
{
return a.x*b.y-a.y*b.x;
}
double dot (point a, point b)
{
return a.x*b.x + a.y*b.y;
}

int ConvexHull (point *p, point *ch, int n)
{
sort (p, p+n);
int m = 0;
for (int i = 0; i < n; i++)
{
while (m > 1 && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
m--;
ch[m++] = p[i];
}
int k = m;
for (int i = n-2; i >= 0; i--)
{
while (m > k && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
m--;
ch[m++] = p[i];
}
if (n > 1)
m--;
return m;
}

int cnt,n;
point p[maxn],ch[maxn];

bool OnSegment (point p, point a1, point a2)//点在线段上(不包含端点)
{
return dcmp (cross (a1-p, a2-p)) == 0 && dcmp (dot (a1-p, a2-p)) < 0;
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
{
cin>>p[i].x>>p[i].y;
p[i].pos=i;
}
if(n<6)
{
printf("NO\n");
continue;
}
else
{
int flag=1;
cnt=ConvexHull(p,ch,n);
if (cnt == 1 || cnt == 2)
{
printf ("NO\n");
continue;
}
for(int i=0; i<cnt; i++)
{
int temp=0;
for(int j=0; j<n; j++)
{
if(p[j].pos!=ch[i].pos&&p[j].pos!=ch[(i+1)%cnt].pos&&OnSegment(p[j],ch[i],ch[(i+1)%cnt]))
{
temp++;
break;
}
}
if(temp==0)
{
flag=0;
break;
}
}
if(flag==1)
printf("YES\n");
else printf("NO\n");

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