您的位置:首页 > 其它

POJ-1228 Grandpa's Estate

2017-07-16 17:54 141 查看
Grandpa's Estate

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

有点难懂,但是还好找到一篇博客写的很不错。http://m.blog.csdn.net/acm_cxq/article/details/51327487

思路给的很好。其他就是改凸包的模版了。
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct T
{
int x,y;
} t[1005],p[1005];

int dis(T a,T b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int m(T a,T b,T c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int cmp(T a,T b)
{
int x=m(a,b,t[0]);
if(x>0||(x==0&&dis(b,t[0])>dis(a,t[0])))return 1;
return 0;
}
int judge(T a,T b,T c)
{
if(m(a,b,c)!=0)
return 0;
if(c.x<=max(a.x,b.x)&&c.x>=min(a.x,b.x)&&c.y<=max(a.y,b.y)&&c.y>=min(a.y,b.y))
return 1;
return 0;
}
int main()
{
int w;
cin>>w;
while(w--)
{
//memset(s,0,sizeof(s));
int n;
scanf("%d",&n);
memset(t,0,sizeof(t));
memset(p,0,sizeof(p));
int k=0;
int j=0;
for(int i=0; i<n; i++)
{
int a,b;
cin>>a>>b;
t[j].x=a;
t[j].y=b;
if(t[j].y<t[k].y||(t[j].y==t[k].y&&t[j].x<t[k].x))k=j;
j++;
}
if(n<6)
{
cout<<"NO"<<endl;
continue;
}
swap(t[k],t[0]);
sort(t+1,t+n,cmp);
p[0]=t[0];
p[1]=t[1];
int top=2;
for(int i=2; i<n; i++)
{
while(top>1&&m(p[top-1],p[top-2],t[i])>=0) top--;
p[top++]=t[i];
}
p[top]=p[0];
if(top<3)
{
cout<<"NO"<<endl;
continue;
}
int flag=0;
for(int i=0; i<top; i++)
{
flag=0;
//cout<<i<<top<<endl;
for(int j=0; j<n; j++)
{
if(t[j].x==p[i].x&&t[j].y==p[i].y)
continue;
if(t[j].x==p[i+1].x&&t[j].y==p[i+1].y)
continue;
if(judge(p[i+1],p[i],t[j]))
{
//cout<<judge(p[i+1],p[i],t[j])<<t[j].x<<t[j].y<<endl;
flag=1;
}
}
if(!flag)
{
flag=-1;
break;
}
}
if(flag==-1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: