题目链接:http://poj.org/problem?id=1696
一只蚂蚁,只能往左拐,求一条路线能让蚂蚁走过所有的点
题目比较简单,每次找最外面一层的,可以直接凸包,每次求凸包
我是直接每次找和上一条路线夹角最小的为下一次的目标点,直到走完所有点!
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
#define maxn 100
struct point{
double x,y;
bool is_use;
int num;
}po[maxn],temp,now;
double angle(point a, point b, point c){
c.x-=(b.x-a.x);
c.y-=(b.y-a.y);
double ux = b.x - a.x, uy = b.y - a.y;
double vx = c.x - a.x, vy = c.y - a.y;
return acos((ux*vx + uy*vy) /
sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));
}
int n,pos;
int main(){
int i,j,k,t;
double ang,re;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%d ",n);
temp.y=100000;
for(i=0;i<n;i++){
scanf("%d%lf%lf",&po[i].num,&po[i].x,&po[i].y);
if(po[i].y < temp.y) pos=i,temp=po[i];
po[i].is_use=true;
}
printf("%d ",po[pos].num);
po[pos].is_use=false;
temp.x-=10;
now=po[pos];
for(i=0;i<n-1;i++){
ang=3.14;
for(j=0;j<n;j++){
if(po[j].is_use==false) continue;
re=angle(temp,now,po[j]);
if(re<ang){
pos=j;
ang=re;
}
}
printf("%d ",po[pos].num);
po[pos].is_use=false;
temp=now;
now=po[pos];
}
printf("\n");
}
return 0;
}