解题思路:其实无论输入什么样的点集,一定可以走完全部n个点的,这是凸包的性质决定 找上一条边的凸组合的第一条边,然后再找这条边的凸组合的第一条边,以此类推
#include <iostream>
#include <string.h>#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 20000
int top = 2;
struct Point
{
double x,y,len;
int id;
} Pt[maxn],Stack[maxn],Point_A;
double Cross( Point a, Point b, Point c)
{
return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x);
}
double Dis(Point a, Point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
void FindPoint( int n)
{
int i,tempNumber = 0;
Point tempPoint ;
Point_A = Pt[0];
for( i = 0; i< n; i++)
{
if(Pt[i].y < Point_A.y || Pt[i].y == Point_A.y && Pt[i].x < Point_A.x)
{
tempNumber = i;
Point_A = Pt[i];
}
}
tempPoint = Pt[0];
Pt[0] = Point_A;
Pt[tempNumber] = tempPoint;
}
bool cmp( Point a, Point b)
{
double k = Cross(Point_A, a, b);
if( k > 0) return true;//顺时针方向
if( k < 0) return false;
a.len = Dis(Point_A,a);
b.len = Dis(Point_A,b);
return a.len < b.len;
}
void Graham( int n)
{
int i;
Stack[0] = Pt[0];
Stack[1] = Pt[1];
// cout<<Pt[1].x<<Pt[2].x<<endl;
for( i = 2; i<n; i++)
{
while(Cross(Stack[top-1],Stack[top],Pt[i]) < 0 && top > 1)
top--;
Stack[++top] = Pt[i];
}
}
int main()
{
int i,Num, t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&Num);
for( i = 0; i < Num; i++)
{
scanf("%d %lf %lf", &Pt[i].id, &Pt[i].x , &Pt[i].y);
}
FindPoint(Num);
//cout<<Point_A.x<<" "<<Point_A.y<<endl;
printf("%d %d",Num, Point_A.id);
for( i = 1 ;i < Num; i++)
{
sort(Pt + i, Pt + Num ,cmp);
printf(" %d",Pt[i].id);
Point_A = Pt[i];
}
printf("\n");
//Graham(Num);
}
return 0;
}