题目链接:https://vjudge.net/problem/POJ-1696
转自:https://blog.csdn.net/yeyun666/article/details/50061923
题意:给多个点,起点是(0,给的点中最小的y),要求只能向左转并且路径不能相交,求最多可以经过的点并输出路径。
思路:首先一定可以经过所有点。 先找出左下角的点,然后不断排序来寻找在它左边最靠近它的点,并且不断更新起点。排序时注意: 1.如果向量a与b叉乘<0,则a在b的顺时针方向 2.如果a叉乘b>0,a在b的逆时针方向 (需注意两向量同起点) 3.如果叉乘==0,说明三点共线,应选距离起点最小的点
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <cmath>
#include <list>
using namespace std;
struct point
{
double x,y;
int id;
point(double a=0,double b=0):x(a),y(b)
{
}
};
const double eps=1e-10;
bool operator ==(point a,point b)
{
return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
}
bool operator <(point a,point b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
point operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
point operator +(point a,point b)
{
return point(a.x+b.x,a.y+b.y);
}
double dist(point a,point b)
{
double x=a.x-b.x;
double y=a.y-b.y;
return sqrt(x*x+y*y);
}
int dcmp(double x)
{
if(fabs(x)<eps)
return 0;
else
return x<0?-1:1;
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
double dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
point p[55];
int pos;
bool cmp(point a,point b)
{
double t=cross(a-p[pos],b-p[pos]);
if(dcmp(t)<0)
{
return false;
}
else if(dcmp(t)>0)
{
return true;
}
else
{
return dist(a,p[pos])<dist(b,p[pos]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%lf%lf",&p[i].id,&p[i].x,&p[i].y);
if(p[i].y<p[0].y)
{
swap(p[i],p[0]);
}
else if(p[i].y==p[0].y)
{
if(p[i].x<p[0].x)
{
swap(p[i],p[0]);
}
}
}
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].id);
}
printf("\n");
}
return 0;
}