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

Grandpa's Estate

 POJ - 1228

题意:给一些点,问能否唯一确定一个凸包。

先求凸包,当且仅当每条边都至少三个点时可唯一确定一个凸包。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=1010;
 7 
 8 struct Node{
 9     int x,y;
10     bool operator < (const Node& a)const{
11         return x<a.x||x==a.x&&y<a.y;
12     }
13     Node operator - (Node& a){
14        return Node{x-a.x,y-a.y};
15     }
16 }p[maxn],ch[maxn];
17 
18 int cross(Node a,Node b){
19     return a.x*b.y-a.y*b.x;
20 }
21 int ConvexHull(int n){
22     if(n<6) return 0;
23     int m=0;
24     for(int i=0;i<n;i++){
25         while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
26         ch[m++]=p[i];
27     }
28     int k=m;
29     for(int i=n-2;i>=0;i--){
30         while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
31         ch[m++]=p[i];
32     }
33     m--;
34     ch[m+1]=ch[0];
35     int i,j;
36     for(i=0;i<m;i++){
37         for(j=0;j<n;j++){
38             if((ch[i].x!=p[j].x||ch[i].y!=p[j].y)&&(ch[i+1].x!=p[j].x||ch[i+1].y!=p[j].y)){
39                 if((ch[i].x-p[j].x)*(p[j].x-ch[i+1].x)>=0&&cross(ch[i]-ch[i+1],ch[i+1]-p[j])==0) break;
40             }
41         }
42         if(j==n) break;
43     }
44     return i>=m;
45 }
46 int main(){
47     int t,n;
48     scanf("%d",&t);
49     while(t--){
50         scanf("%d",&n);
51         for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
52         sort(p,p+n);
53         int ans=ConvexHull(n);
54         if(ans) puts("YES");
55         else puts("NO");
56     }
57     return 0;
58 }
View Code

 

凸包一开始有个地方写错了,,判断三个点的地方也一直有问题,最后还是看的别人的~

爆炸。。。

 

下面这种方法更好

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=1010;
 7 
 8 struct Node{
 9     int x,y;
10     bool operator < (const Node& a)const{
11         return x<a.x||x==a.x&&y<a.y;
12     }
13     Node operator - (Node& a){
14        return Node{x-a.x,y-a.y};
15     }
16 }p[maxn],ch[maxn];
17 
18 int cross(Node a,Node b){
19     return a.x*b.y-a.y*b.x;
20 }
21 int ConvexHull(int n){
22     if(n<6) return 0;
23     sort(p,p+n);
24     int m=0;
25     for(int i=0;i<n;i++){
26         while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;  //这里凸包边上的点也要保留
27         ch[m++]=p[i];
28     }
29     int k=m;
30     for(int i=n-2;i>=0;i--){
31         while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;  //
32         ch[m++]=p[i];
33     }
34     if(n>1) m--;
35     return m;
36 }
37 bool check(int n){
38     if(n==0) return 0;
39     for(int i=0;i<=n;i++){
40         if(cross(ch[i]-ch[(i-1+n)%n],ch[(i+1)%n]-ch[i])!=0&&cross(ch[(i+1)%n]-ch[i],ch[(i+2)%n]-ch[i])!=0)
41             return 0;
42     }
43     return 1;
44 }
45 int main(){
46     int t,n;
47     scanf("%d",&t);
48     while(t--){
49         scanf("%d",&n);
50         for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
51         int m=ConvexHull(n);
52         int ans=check(m);
53         if(ans) puts("YES");
54         else puts("NO");
55     }
56     return 0;
57 }
View Code

 

posted @ 2017-08-16 23:18  yijiull  阅读(143)  评论(0编辑  收藏  举报