poj 1696 Space Ant
#include<stdio.h> #include<algorithm> using namespace std; const int Max=55; struct Point { int x,y,num; } p[Max],res[Max]; int pos; int xmult(Point p1,Point p2,Point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } int dis(Point p1,Point p2) { return (p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y); } bool cmp(Point p1,Point p2) { int tmp=xmult(p1,p2,p[pos]); if(tmp>0) return true; if(!tmp&&dis(p[pos],p1)<dis(p[pos],p2)) return true; return false; } int main() { int _case,n,i; scanf("%d",&_case); while(_case--) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d%d",&p[i].num,&p[i].x,&p[i].y); if(p[i].y<p[0].y||p[i].y==p[0].y&&p[i].x<p[0].x) swap(p[i],p[0]); } for(i=1;i<n;i++) { pos=i-1; sort(p+i,p+n,cmp); } printf("%d ",n); for(i=0;i<n-1;i++) printf("%d ",p[i].num); printf("%d\n",p[n-1].num); } }