Grandpa's Estate
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 |
题目:http://poj.org/problem?id=1228
题意:有一个n个点的凸多边形,问这个多边形是否是唯一确定的,(题目描述大概就是这样的,没有解释什么叫做唯一确定,看了别人的解释后,你可以简单的认为只要每条边上的点都大于两个就行。
分析:弄清题意后,还是比较麻烦的,首先,之前我用的凸包模板就有多余的点,改正后,又不好确定每条边上的点数,后来冥思苦想,发现扫描一遍就行了,因为求凸包的那个极角排序已经将同一条边上的点排在相邻的位置,扫描的时候统计每条边的点数即可,具体看代码吧
还有所有点在同一条边上算NO
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=1111;
typedef int mType;
struct Tpoint
{
mType x,y;
Tpoint(){}
Tpoint(mType _x,mType _y):x(_x),y(_y){}
}g[mm],q[mm];
Tpoint MakeVector(Tpoint P,Tpoint Q)
{
return Tpoint(Q.x-P.x,Q.y-P.y);
}
mType CrossProduct(Tpoint P,Tpoint Q)
{
return P.x*Q.y-P.y*Q.x;
}
mType MultiCross(Tpoint P,Tpoint Q,Tpoint R)
{
return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));
}
mType SqrDis(Tpoint P,Tpoint Q)
{
return (P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y);
}
bool TurnRight(Tpoint P,Tpoint Q,Tpoint R)
{
mType tmp=MultiCross(P,Q,R);
if(tmp>0)return 1;
if(tmp<0)return 0;
return SqrDis(P,Q)<SqrDis(P,R);
}
bool cmp(Tpoint P,Tpoint Q)
{
mType tmp=MultiCross(P,Q,g[0]);
if(tmp>0)return 0;
if(tmp<0)return 1;
return SqrDis(P,g[0])>SqrDis(Q,g[0]);
}
void Graham(int n,int &m)
{
int i,j;
for(j=i=0;i<n;++i)
if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i;
swap(g[0],g[j]);
sort(g+1,g+n,cmp);
q[m=0]=g[n]=g[0];
for(i=1;i<=n;++i)
{
while(m&&TurnRight(q[m-1],q[m],g[i]))--m;
q[++m]=g[i];
}
}
bool IsAllOnLine(int n)
{
for(int i=2;i<n;++i)
if(MultiCross(g[0],g[1],g[i]))return 0;
return 1;
}
int main()
{
int i,j,k,n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d%d",&g[i].x,&g[i].y);
if(n<6||IsAllOnLine(n))puts("NO");
else
{
Graham(n,m);
for(j=i=0;i<m;++i)
{
k=0;
while(!MultiCross(q[i],q[i+1],g[j]))++k,++j;
if(k<2)break;
}
puts(i<m?"NO":"YES");
}
}
return 0;
}