Space Ant 极角排序

题意:给定n个点,任意两点不在一个x轴和y轴上,输出一条能经过最多点的路线,就下面这图,每次只能左转,实际就是外面一圈包进去,

选择一个最外侧点,对剩下点做极角排序,一个个点选下去。能保证每个点都经过。

 

 

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<limits.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<memory>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<utility>
using namespace std;
#define mp make_pair
#define ss second
#define ff first
#define pb push_back
template <class T>
void read(T &x)
{
    static char ch;
    bool neg = false;
    while(!isdigit(ch = getchar())) (ch == '-') && (neg = true);
    for(x = ch - '0'; isdigit(ch = getchar()); x = x * 10 + ch - '0');
    (neg) &&(x = -x);
}
template<class T, class... U> void read(T &head, U &... tail){read(head), read(tail...);}
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 105;
int n,m;
struct P
{
    double x, y;
    int ind;
} v[maxn];
bool vis[maxn];
double compare(P a, P b, P c)
{
    return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
P c;
bool cmp(P a, P b)
{
    if(compare(c, a, b) == 0)
        return a.x < b.x;
    else
        return compare(c, a, b) > 0;
}
bool cmp1(P a,P b)
{
    return a.y<b.y;
}
std::vector<P> w;
std::vector<int> q;
void slove()
{
    c=w[0];
    q.push_back(c.ind);
    vis[c.ind]=true;
    w.clear();
    for(int i=1;i<n;i++)
    {
        if(!vis[v[i].ind])
        w.push_back(v[i]);
    }
    if(w.size()==0) return;
    sort(w.begin(),w.end(),cmp);
    slove();
}
void work()
{
    read(n);
    q.clear();
    w.clear();
    memset(vis,false,sizeof vis);
    for(int i=0;i<n;i++) 
        scanf("%d %lf %lf",&v[i].ind,&v[i].x,&v[i].y);
    sort(v,v+n,cmp1);
    c=v[0];
    q.push_back(c.ind);
    vis[c.ind]=true;
    for(int i=1;i<n;i++)
    {
        if(!vis[v[i].ind])
        w.push_back(v[i]);
    }
    sort(w.begin(),w.end(),cmp);  
    
    slove();
    printf("%d",n);
    for(int i=0;i<n;i++)
        printf(" %d",q[i]);
    printf("\n");


}

int main()
{
    int T;read(T);while(T--)
    //while(~scanf("%d", &n))
    work();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值