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
思路给的很好。其他就是改凸包的模版了。
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; } }
相关文章推荐
- POJ 1228 Grandpa's Estate(确定凸包)
- POJ 1228 Grandpa's Estate(判断是否稳定凸包)
- Poj 1228 Grandpa's Estate (凸包及唯一性判定)
- poj 1228 Grandpa's Estate 凸包模板题
- POJ1228-Grandpa's Estate
- POJ 1228 Grandpa's Estate
- Grandpa's Estate - POJ 1228(稳定凸包)
- POJ 1228 Grandpa's Estate 计算凸包+判断点在线段上
- POJ 1228 Grandpa's Estate
- POJ 1228 Grandpa's Estate(凸包应用:稳定凸包)
- POJ 1228 Grandpa's Estate (求稳定凸包)
- POJ 1228 Grandpa's Estate(稳定凸包)
- poj 1228 Grandpa's Estate (凸包)
- POJ 1228 Grandpa's Estate(给定凸包上的一部分顶点 判断表示的凸包是否唯一)
- [POJ 1228] Grandpa's Estate · 凸包
- POJ 1228 Grandpa's Estate
- POJ 1228 Grandpa's Estate 【计算几何:凸包,andrew】
- POJ 1228 Grandpa's Estate
- POJ 1228 Grandpa's Estate (凸包唯一性判定 推荐)
- poj - 1228 - Grandpa's Estate