您的位置:首页 > 其它

Space Ant--poj1696(极角排序)

2016-05-10 15:32 92 查看
http://poj.org/problem?id=1696

极角排序是就是字面上的意思 按照极角排序

题目大意:平面上有n个点然后有一只蚂蚁他只能沿着点向左走 求最多能做多少点

分析: 其实还不知道极角排序到底是什么, 但是又好像知道一点 必须一直排序 然后一直找到最左的点就行了

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<vector>
using namespace std;
#define INF 0xfffffff
#define ESP 1e-8
#define memset(a,b) memset(a,b,sizeof(a))
#define N 2100

struct Point
{
double x,y;
int index;
Point(double x=0,double y=0):x(x),y(y){}
Point operator - (const Point &temp)const{
return Point(x-temp.x,y-temp.y);
}
Point operator + (const Point &temp)const{
return Point(x+temp.x,y+temp.y);
}
int operator ^(const Point &temp)const{///求叉积
double t=(x*temp.y)-(y*temp.x);
if(t>ESP)
return 1;
if(fabs(t)<ESP)
return 0;
return -1;
}
double operator * (const Point &temp)const{
return x*temp.x+y*temp.y;
}
bool operator == (const Point &temp)const{
return (x==temp.x)&&(y==temp.y);
}
}p
;

double dist(Point a1,Point a2)
{
return sqrt((a1-a2)*(a1-a2));
}
int pos=0;

int cmp(Point a1,Point a2)
{
int t=(a1-p[pos])^(a2-p[pos]);
if(t==0)
return dist(p[pos],a1) < dist(p[pos],a2);
else if(t<0) return false;
else
return true;
}

int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %lf %lf",&p[i].index,&p[i].x,&p[i].y);
if(p[i].y<p[0].y || (p[i].y==p[0].y && 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].index);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: