题意:有一种蚂蚁,他只能向左转,他还要吃植物,给你一个图,图上有好多植物的坐标,为找到一条路径使得这只蚂蚁吃得最多的植物
思路:极角排序,选定一个最外围的一个点,怎么排序呢?
三个点的叉乘可以确定哪条直线在右面,一个点在另一点的右面,说明这个点更靠外面,就是根据这个叉乘排序选择第一个,然后再根据选择的点在排序,在选择第一个,这样一直选择下去
c++忘完了快,写了个三个参数的构造函数,当传2个参数时,他把我的第一个参数覆盖掉了,让我好找啊....惨痛的教训
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
int Num;
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
//Point(int num = 0,double x = 0,double y = 0):Num(num),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 - (Vector a, Vector 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) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-8;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
Point p[100];
int pos;
bool cmp(Point a,Point b)
{
double tmp = Cross(a-p[pos],b-p[pos]);
if(dcmp(tmp) > 0)return true;
if(dcmp(tmp) == 0)
return Length(p[pos]-a) < Length(p[pos]-b);
else return false;
}
int main()
{
int T;
scanf("%d",&T);
int n;
while(T--)
{
scanf("%d",&n);
double x,y;
for(int j = 0;j< n;j++)
{
scanf("%d%lf%lf",&p[j].Num,&p[j].x,&p[j].y);
if( p[j].y < p[0].y || (p[j].y == p[0].y && p[j].x < p[0].x) )
swap(p[0],p[j]);
}
pos = 0;
for(int i = 1;i < n;i++)
{
sort(p+i,p+n,cmp);
pos++;
}
printf("%d",n);
for(int i = 0;i < n;i++)
printf(" %d",p[i].Num);
printf("\n");
}
return 0;
}