先不会,后来看了discus才知道,用极坐标排序,换句话说,按着偏转的角度从小到大,相同的按距离从小到大。最后形成的路线是一个螺旋形的折线。
#include<cstdio>
#include<algorithm>
using namespace std;
struct node {
int id,x,y;
}p[105];
node rec[105];
int pos,cnt;
int Judge(node a,node b,node c){
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int dis(node a,node b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(node &a,node &b){
int t;
t=Judge(p[pos],a,b);
if(t>0) return true;
else if(!t&&dis(p[pos],a)<dis(p[pos],b)) return true;
return false;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d %d %d",&p[i].id,&p[i].x,&p[i].y);
if(p[i].y<p[0].y) swap(p[i],p[0]);
}
cnt=pos=0;
sort(p+pos,p+n,cmp);
rec[cnt++]=p[pos++];
for(i=2;i<n;i++){
sort(p+pos,p+n,cmp);
rec[cnt++]=p[pos++];
}
rec[cnt++]=p[pos++];
printf("%d",cnt);
for(i=0;i<cnt;i++){
printf(" %d",rec[i].id);
}
printf("\n");
}
return 0;
}