您的位置:首页 > 其它

Grandpa's Estate - POJ 1228(稳定凸包)

2015-10-25 11:45 309 查看
刚开始看这个题目不知道是什么东东,后面看了大神的题解才知道是稳定凸包问题,什么是稳定凸包呢?所谓稳定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点。知道了这个东西就简单了,直接求出来凸包后,然后判断每条边上的点是否超过三点就行了。

代码如下:

============================================================================================================================

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

const double EPS = 1e-16;
const int MAXN = 1007;
const int oo = 1e9+7;

int sta[MAXN], top;

struct point
{
double x, y;

point(double x=0, double y=0):x(x), y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}
double operator *(const point &t)const{
return x*t.x + y*t.y;
}
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}
}p[MAXN];
int Sign(double t)
{
if(t > EPS)return 1;
if(fabs(t) < EPS)return 0;
return -1;
}
double Dist(point a, point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
int t = Sign((a-p[0])^(b-p[0]));

if(t == 0)
return Dist(a, p[0]) < Dist(b, p[0]);
return t > 0;
}
void Graham(int N)
{
int k=0;

for(int i=0; i<N; i++)
{
if(p[k].y>p[i].y || (Sign(p[k].y-p[i].y)==0 && p[k].x > p[i].x))
k = i;
}
swap(p[0], p[k]);
sort(p+1, p+N, cmp);

sta[0]=0, sta[1]=1, top=1;

if(N < 2)
{
top = N-1;
return ;
}

for(int i=2; i<N; i++)
{
while(top>0 && Sign((p[i]-p[sta[top]])^(p[sta[top-1]]-p[sta[top]])) <= 0)
top--;
sta[++top] = i;
}
}

int main()
{
int T, N, i, j;

scanf("%d", &T);

while(T--)
{
scanf("%d", &N);

for(i=0; i<N; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);

Graham(N);
sta[++top] = sta[0];

for(i=0; i<top; i++)
{
int s=sta[i], e=sta[i+1];
for(j=0; j<N; j++)
{
if(j==s || j==e)
continue;
if(Sign( (p[s]-p[e])^(p[j]-p[e]) ) == 0)
break;
}

if(j == N)
break;
}

if(i < top || top < 3)
printf("NO\n");
else
printf("YES\n");
}

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