极角排序:POJ1696

POJ1696

题意:

  • 给你平面的一个点集,然后要你从y坐标最小的点开始以水平方向向下一个点连线,每次只能直走或左转,问你最多能走多少点?

题解:极角排序

  • 先以y为基准排序,找到y最小的点最为起始点。
  • 每次以基准点进行极角排序,找到写一个点,一次类推。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
int const N = 100 + 10;
typedef struct Point{
    int num;
    double x,y;
    Point(){};
    Point(double x,double y):x(x),y(y){};
    Point operator - (const Point& p)const{
        return Point(x - p.x,y - p.y);
    }
    double operator ^ (const Point& p)const{
        return x * p.y - y * p.x;
    }
}Vector;
Point p[N],st;
bool cmp1(Point a,Point b){
    return a.y < b.y || a.y == b.y && a.x < b.x;
}
bool cmp2(Point a,Point b){
    double t = (a - st) ^ (b - st);
    return t > 0 || t == 0 && a.x < b.x;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%lf%lf",&p[i].num,&p[i].x,&p[i].y);
        }
        sort(p,p + n,cmp1);
        for(int i=1;i<n;i++){
            st = p[i-1];    //以p[i-1]为基准
            sort(p + i,p + n,cmp2);
        }
        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、付费专栏及课程。

余额充值