Poj 1696 Space Ant 【极角排序】

来源:互联网 发布:sas高校数据分析大赛 编辑:程序博客网 时间:2024/06/03 23:05



点击打开链接


题意:


         一个蚂蚁吃东西,给出n个东西的坐标,蚂蚁能从任意起点开始

         1、蚂蚁不会右转。

         2、蚂蚁走过的地方不会再走。

         问蚂蚁最多能吃那些东西,输出吃东西的顺序。


题解:


         很容易想到逆时针吃肯定能吃完。

         那么就找到最最下角的点作为起点,然后每次走都找到角度偏斜最小的,相等的话距离最近的。



#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<map>#include<cmath>#include<vector>#define x first#define y secondusing namespace std;inline int read(){int x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=x*10+c-'0';return x*f;}inline void out(int x){if(x>9) out(x/10);putchar(x%10+'0');}const double eps=1e-8;const double PI=acos(-1.0);const int maxn=1e5+10;const int inf=0xffffff;int pos;int sgn(double x){    if(fabs(x)<eps) return 0;    if(x<0) return -1;    return 1;}struct Point{    double x,y;    int id;    Point(){}    Point(double _x,double _y){        x=_x;y=_y;    }    Point operator -(const Point &b)const{        return Point(x-b.x,y-b.y);    }    Point operator +(const Point &b)const{        return Point(x+b.x,y+b.y);    }    double operator ^ (const Point &b)const{        return x*b.y - y*b.x;    }    double operator * (const Point &b)const{        return x*b.x + y*b.y;    }};struct Line{    Point s,e;    Line(){}    Line(Point _s,Point _e){        s=_s;e=_e;    }};bool inter(Line l1,Line l2){    return    max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&    max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&    max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&    max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0&&    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0;}bool OnSeg(Point P,Line L){    return    sgn((L.s-P)^(L.e-P))&&    sgn((P.x-L.s.x)*(P.x-L.e.x))<=0&&    sgn((P.y-L.s.y)*(P.y-L.e.y))<=0;}double dist(Point a,Point b){    return sqrt((a-b)*(a-b));}Point p[105];bool cmp(Point a,Point b){    double tmp = (a-p[pos])^(b-p[pos]);    if(sgn(tmp) == 0)        return dist(p[pos],a) < dist(p[pos],b);    else if(sgn(tmp) < 0)return false;    else return true;}bool v[maxn];int main(){    int t,n,cnt;    double x1,x2,x3,x4,y1,y2,y3,y4;    scanf("%d",&t);    while(t--){        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||(p[i].y==p[0].y&&p[i].x<p[0].x))                swap(p[0],p[i]);        }        pos=0;        for(int i=0;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);        puts("");    }return 0;}


        

原创粉丝点击