题目链接:
http://poj.org/problem?id=1696
这道题直接就是凸包模板了。。我用的卷包裹法
AC代码:
#include<stdio.h> #include<algorithm> #include<cstring> #include<iostream> using namespace std; #define inf 9999999 struct point { int x,y; }; point p[55]; //叉积计算 int det(int x1,int y1,int x2,int y2) { return x1*y2-x2*y1; } int cross(point A,point B,point C,point D) { return det(B.x-A.x,B.y-A.y,D.x-C.x,D.y-C.y); } int dist(point A,point B) { return(B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y); } int main() { int m,n,num; int k,s,tot; int minx,miny,vis[55]; scanf("%d",&m); int conbag[55]; while(m--) { minx=inf; miny=inf; memset(vis,0,sizeof(vis)); memset(conbag,0,sizeof(conbag)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%d",&num,&p[i].x,&p[i].y); if(p[i].y<miny) { minx=p[i].x; miny=p[i].y; s=i; } } tot=0; vis[s]=1; conbag[0]++; conbag[++tot]=s; while(true) { s=conbag[tot]; for(int i=1;i<=n;i++) { if(!vis[i]) { k=i; break; } } for(int i=1;i<=n;i++) { if(k!=i&&!vis[i]) { int temp=cross(p[s],p[k],p[s],p[i]); if(temp<0) k=i; else if(temp==0) { if(dist(p[s],p[i])<dist(p[s],p[k])); k=i; } } } conbag[++tot]=k; conbag[0]++; vis[k]=1; if(n-conbag[0]==1) break; } printf("%d ",conbag[0]+1); int k=0; for(int i=1;i<=n-1;i++) { printf("%d ",conbag[i]); if(!vis[i]) k=i; } if(k) printf("%d\n",k); else printf("%d\n",n); } }