Space Ant(poj1410叉积的极角排序)

题意:有一种蚂蚁,他只能向左转,他还要吃植物,给你一个图,图上有好多植物的坐标,为找到一条路径使得这只蚂蚁吃得最多的植物

思路:极角排序,选定一个最外围的一个点,怎么排序呢?

三个点的叉乘可以确定哪条直线在右面,一个点在另一点的右面,说明这个点更靠外面,就是根据这个叉乘排序选择第一个,然后再根据选择的点在排序,在选择第一个,这样一直选择下去

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值