POJ 1696 Space Ant【计算几何】

题面

题意: 给出一系列二维点,问从(0,y)出发,y是这些点里纵坐标的最小值,只能左转或者直走,并且每个点只能走一次,输出能走完这些点的路径的顺序。

题解: 先找到第一个点,它肯定是当y取最小时的那个点,然后接下来开始遍历,每次找的点与pre连成的线都要与pre和first连成的线的夹角最小,找到后就更新first为pre,更新pre为当前的找到的点

// #include<bits/stdc++.h>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;    
const double pi = acos((double)(-1));
#define inf 0x3f3f3f3f
#define ll long long
#define eps 1e-8
const int maxn = 60;
const int mod = 1e9 + 7;
int sgn(double x){                 //判断x是否为0
    if(fabs(x) < eps) return 0;
    else return x < 0?-1:1;
}
int dcmp(double x, double y){      //比较两个浮点数   
    if(fabs(x - y) < eps) return 0;
    else return x < y?-1:1;
}
struct Point{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    Point operator + (Point B){return Point(x + B.x,y + B.y);}
    Point operator - (Point B){return Point(x - B.x,y - B.y);}
    Point operator * (double k){return Point(x*k,y*k);}
    Point operator / (double k){return Point(x/k,y/k);}
    bool operator == (Point B){return sgn(x - B.x) == 0 && sgn(y - B.y) == 0;}
}P[maxn];
typedef Point Vector;   //用点的数据结构定义向量
struct Line{
    Point p1,p2;
    Line(){}
    Line(Point p1,Point p2):p1(p1),p2(p2){}
    Line(Point p,double angle){    //y = kx + b
        p1 = p;
        if(sgn(angle - pi/2) == 0){p2 = (p1 + Point(0,1));}
        else {p2 = (p1 + Point(1,tan(angle)));}
    }
    Line(double a,double b,double c){    //ax + by + c = 0
        if(sgn(a) == 0){
            p1 = Point(0, -c/b);
            p2 = Point(1, -c/b);
        }
        else if(sgn(b) == 0){
            p1 = Point(-c/a,0);
            p2 = Point(-c/a,1);
        }
        else{
            p1 = Point(0,-c/b);
            p2 = Point(1,(-c - a)/b);
        }
    }
}L[maxn];
bool cmp(const Line &v1, const Line &v2){
    if(v1.p1.x != v2.p1.x) return v1.p1.x < v2.p1.x;
    return v1.p1.y < v2.p1.y;
}
typedef Line Segment;
double Dist(Point A,Point B){
    return sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}
double Dot(Vector A,Vector B){return A.x*B.x + A.y*B.y;}  //计算向量点积,>0锐角,<0钝角,=0直角 A·B = fabs(A)*fabs(B)*cos(angle)
double Len(Vector A){return sqrt(Dot(A,A));}             //计算向量长度
double Len2(Vector A){return Dot(A,A);}                  //向量长度的平方
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Len(A)/Len(B));}   //计算两向量的夹角
double Cross(Vector A,Vector B){return A.x*B.y - A.y*B.x;}  //计算向量叉积,叉积有正负,A×B>0(B在A的逆时针方向),B×A<0   A×B = fabs(A)*fabs(B)*sin(angle)
double Area(Vector A,Vector B,Vector C){return Cross(B- A,C - A);} //以A为公共点的两向量构成的平行四边形面积
Vector Rotate(Vector A,double rad){          //计算一个点逆时针选择rad度后的点
    return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}
Vector Normal(Vector A){return Vector(-A.y/Len(A),A.x/Len(A));}   //求该向量的法向量
bool Parallel(Vector A,Vector B){return sgn(Cross(A,B)) == 0;}  //用叉积判断两向量是否平行或重合
int Point_line_relation(Point p, Line v){                       //点和线的位置关系
    int c = sgn(Cross(p - v.p1, v.p2 - v.p1));
    if(c < 0) return 1;         //p在v的左边(上面)
    if(c > 0) return 2;         //p在v的右边(下面)
    return 0;
}
bool Point_on_seg(Point p, Line v){  //0为不在线段上,1为在线段上
    return sgn(Cross(p - v.p1, v.p2 - v.p1)) == 0 && sgn(Dot(p - v.p1, p - v.p2)) <=0;
}
double Dis_point_line(Point p, Line v){     //点到直线的距离
    return fabs(Cross(p - v.p1, v.p2 - v.p1))/Dist(v.p1, v.p2);
}
Point Point_line_proj(Point p, Line v){        //点在直线上的投影
    double k = Dot(v.p2 - v.p1, p - v.p1)/Len2(v.p2 - v.p1);
    return v.p1 + (v.p2 - v.p1) * k;
}
Point Point_line_symmetry(Point p, Line v){       //点关于直线的对称点
    Point q = Point_line_proj(p, v);
    return Point(2 * q.x - p.x, 2 * q.y - p.y);
}
double Dis_point_seg(Point p, Segment v){           //点到线段的距离
    if(sgn(Dot(p - v.p1, v.p2 - v.p1)) < 0 || sgn(Dot(p - v.p2, v.p1 - v.p2)) < 0)
        return min(Dist(p, v.p1), Dist(p, v.p2));
    return Dis_point_line(p, v);
}
int Line_relation(Line v1, Line v2){         //两条直线的位置关系
    if(sgn(Cross(v1.p2 - v1.p1, v2.p2 - v2.p1)) == 0){
        if(Point_line_relation(v1.p1, v2) == 0)  return 1;         //重合
        else return 0;                                             //平行
    }
    return 2;                                                      //相交
}
Point Cross_point(Point a, Point b, Point c, Point d){           //求两条直线的交点,Line1:ab,Line2:cd,两直线不平行不共线
    double s1 = Cross(b - a, c - a);
    double s2 = Cross(b - a, d - a);
    return Point(c.x * s2 - d.x * s1, c.y * s2 - d.y * s1) / (s2 - s1);
}
bool Cross_segment(Point a, Point b, Point c, Point d){               //两线段是否规范相交 即如果一条线段的一个端点恰在另一条线段上则不视为相交;如果两条线段部分重合,也不视为相交。
    double c1 = Cross(b - a, c - a), c2 = Cross(b - a, d - a);
    double d1 = Cross(d - c, a - c), d2 = Cross(d - c, b - c);
    return sgn(c1) * sgn(c2) < 0 && sgn(d1) * sgn(d2) < 0;            //1:相交,0:不相交,端点重合不相交
}
bool Cross_segment1(Point a, Point b, Point c, Point d){    //两线段是否非规范相交
    return 
    max(a.x, b.x) >= min(c.x, d.x)&&
    max(c.x, d.x) >= min(a.x, b.x)&&
    max(a.y, b.y) >= min(c.y, d.y)&&
    max(c.y, d.y) >= min(a.y, b.y)&&
    sgn(Cross(b - a, c - a)) * sgn(Cross(b - a, d - a)) <= 0&&
    sgn(Cross(d - c, a - c)) * sgn(Cross(d - c, b - c)) <= 0;
    sgn(Cross(c - b, a - b)) * sgn(Cross(d - b, a - b)) <= 0&&
    sgn(Cross(a - d, c - d)) * sgn(Cross(b - d, c - d)) <= 0;
}
double Helen(double a, double b, double c){
    double p = (a + b + c)/2;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}
int t, n, num, vis[maxn];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        memset(vis, 0, sizeof(vis));
        double boty = inf;
        double minn = inf, angle = 0;
        int pre= 0, first = 0;
        for(int i = 1; i <= n; i++){
            cin>>num>>P[i].x>>P[i].y; 
            if(P[i].y < boty){ 
                boty = P[i].y;
                pre = i;
            }
        }
        P[0] = {0, boty};
        cout<<n<<" "<<pre;
        vis[pre] = 1;
        int N = n;
        n--;
        int last = 0;
        while(n--){
            minn = inf;
            for(int i = 1; i <= N; i++){
                if(vis[i]) continue;
                angle = Angle(P[pre] - P[first], P[i] - P[pre]);
                if(angle - minn< eps){
                    minn = angle;
                    last = i;
                }
            }
            vis[last] = 1;
            first = pre;
            pre = last;
            cout<<" "<<pre;
        }
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值