计算几何凸包,理解的不是很透彻,还是为什么要用凸包而不是一般的凸多边形这一问题,代码的准确率还是很低,以后要仔细仔细再仔细
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; const int maxn=1000+10; struct Point { int x,y; }; Point po[maxn]; int vis[maxn]; int n; int cou[maxn]; int ch[maxn]; int cross(Point a,Point b,Point c,Point d) { return (b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x); } int Andrew() { if(n<6) return 0; int m=0; int i; for(i=0;i<n;i++) { while(m>1&&cross(po[ch[m-2]],po[ch[m-1]],po[ch[m-1]],po[i])<=0) { vis[ch[m-1]]=0; m--; } ch[m++]=i; vis[ch[m-1]]=1; } int k=m; for(i=n-2;i>=0;i--) { if(i==0||!vis[i])////////////////////////////////////// { while(m>k&&cross(po[ch[m-2]],po[ch[m-1]],po[ch[m-1]],po[i])<=0) { vis[ch[m-1]]=0;// m--; } ch[m++]=i;vis[ch[m-1]]=1; } } int j; if(m<=3) return 0; for(i=0;i<n;i++) { if(!vis[i]) { for(j=0;j<m-1;j++) { if(cross(po[i],po[ch[j]],po[ch[j]],po[ch[j+1]])==0) { cou[ch[j]]++;//////////////////////////////////////// break; } } if(j==m-1) return 0; } } for(j=0;j<m-1;j++) { if(cou[ch[j]]<1) return 0; } return 1; } bool cmp(Point a,Point b) { if(a.x>b.x) return 0; else if(a.x<b.x) return 1; else return a.y<b.y; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); int i; for(i=0;i<n;i++) scanf("%d %d",&po[i].x,&po[i].y); memset(vis,0,sizeof(vis)); memset(cou,0,sizeof(cou)); sort(po,po+n,cmp); int flag=Andrew(); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }