题目大意:
有M组样例
每组样例N个点
每个点为 编号 X Y
每天只能左转移动一点
求点的编号顺序
思路:
从x最小的点对应在y轴上的投影为起点
做卷包裹法不返回起点即可
每添加一点标记下
直到所有点都走完
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps=1e-7;//精度
const int INF=1<<29;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}//向量+向量=向量
Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}//点-点=向量
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p);}//向量*实数=向量
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p);}//向量/实数=向量
bool operator<(const Point&a,const Point&b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int doublecmp(double x){//判断double等于0或。。。
if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
bool operator==(const Point&a,const Point&b){
return doublecmp(a.x-b.x)==0&&doublecmp(a.y-b.y)==0;
}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}//|a|*|b|*cosθ 点积
double Length(Vector a){return sqrt(Dot(a,a));}//|a| 向量长度
double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));}//向量夹角θ
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}//叉积 向量围成的平行四边形的面积
double Area2(Point a,Point b,Point c){return Cross(b-a,c-a);}//同上 参数为三个点
Vector Rotate(Vector a,double rad){//向量旋转rad弧度
return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Vector Normal(Vector a){//计算单位法线
double L=Length(a);
return Vector(-a.y/L,a.x/L);
}
Point GetLineProjection(Point p,Point a,Point b){
Vector v=b-a;
return a+v*(Dot(v,p-a)/Dot(v,v));
}
Point GetLineIntersection(Point p,Vector v,Point q,Vector w){//求直线交点 有唯一交点时可用
Vector u=p-q;
double t=Cross(w,u)/Cross(v,w);
return p+v*t;
}
//直线P+tV
//--------------------------------------
int vis[55];
struct node{
int pos;
Point p;
bool friend operator<(node a,node b){
return a.p<b.p;
}
}arr[55];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
printf("%d",n);
double star=INF;
arr[0].pos=0;
for(int i=1;i<=n;i++){
arr[i].pos=0;
scanf("%*d%lf%lf",&arr[i].p.x,&arr[i].p.y);
star=min(star,arr[i].p.y);
}
arr[0].p.x=0;arr[0].p.y=star;
int count=1;
Vector curv(1,0);
Point curp=arr[0].p;
while(count<=n){
double minjiao=INF;
double mindis=INF;
int purpose=-1;
for(int i=1;i<=n;i++)if(!arr[i].pos){
double jiao=Angle(curv,arr[i].p-curp);
double dis=Length(arr[i].p-curp);
if(jiao<minjiao||(doublecmp(jiao-minjiao)==0&&dis<mindis)){
mindis=dis;minjiao=jiao;
purpose=i;
}
}
curv=arr[purpose].p-curp;
curp=arr[purpose].p;
arr[purpose].pos=count++;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)if(arr[j].pos==i)
printf(" %d",j);
printf("\n");
}
return 0;
}