POJ 1696 Space Ant

极角排序

每次选择一个最外围的没选过的点,选择的时候需要利用极角排序进行选择

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;

const double eps=1e-8;
struct point
{
    int x,y;
    double alpha;
    int len2;
    int id;
} p[100];
int T,n;
vector<point>v;
vector<int>ans;
bool flag[100];

bool cmp(const point &a, const point &b)
{
    if(fabs(a.alpha-b.alpha)<eps) return a.len2<b.len2;
    return a.alpha<b.alpha;
}

int len2(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

double f(point a,point b,point c) //返回点a所对应的角的弧度
{
    double B2=(double)len2(a,c);
    double C2=(double)len2(a,b);
    double A2=(double)len2(b,c);
    double COSA=(B2+C2-A2)/(2*sqrt(B2)*sqrt(C2));
    return 3.1415926-acos(COSA);
}

int main()
{
    scanf("%d",&T);
    int Miny=999999;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&p[i].id,&p[i].x,&p[i].y);
            Miny=min(Miny,p[i].y);
        }

        memset(flag,0,sizeof flag);
        ans.clear();

        point pre;pre.x=-100;pre.y=Miny;
        point now;now.x=0;now.y=Miny;

        for(int i=1; i<=n; i++)
        {
            v.clear();
            for(int k=1; k<=n; k++)
            {
                if(!flag[p[k].id])
                {
                    p[k].alpha=f(now,p[k],pre);
                    p[k].len2=len2(p[k],now);
                    v.push_back(p[k]);
                }
            }
            sort(v.begin(),v.end(),cmp);
            flag[v[0].id]=1;
            ans.push_back(v[0].id);
            pre.x=now.x;
            pre.y=now.y;
            now.x=v[0].x;
            now.y=v[0].y;
        }
        printf("%d ",ans.size());
        for(int i=0; i<ans.size(); i++)
        {
            printf("%d",ans[i]);
            if(i<ans.size()-1) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}
时间: 2024-12-22 21:49:35

POJ 1696 Space Ant的相关文章

POJ 1696 Space Ant(点积的应用)

Space Ant 大意:有一只蚂蚁,每次都只向当前方向的左边走,问蚂蚁走遍所有的点的顺序输出.开始的点是纵坐标最小的那个点,开始的方向是开始点的x轴正方向. 思路:从开始点开始,每次找剩下的点中与当前方向所形成的夹角最小的点,为下一个要走的点(好像就是犄角排序,我不是很会),夹角就是用点积除以两个向量的距离,求一下acos值. 之前一直用叉积做,做了好久样例都没过,发现用错了... 题目挺好的,有助于理解点积与叉积 1 struct Point{ 2 double x, y; 3 int id

poj 1696 Space Ant (极角排序)

链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3077   Accepted: 1965 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an a

poj 1696 Space Ant(极角排序)

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3661   Accepted: 2281 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

poj 1696 Space Ant(模拟+叉积)

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3840   Accepted: 2397 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

POJ 1696 Space Ant (极角排序)

题目链接 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3219   Accepted: 2059 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the plane

POJ 1696 Space Ant 卷包裹法

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3316   Accepted: 2118 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

POJ 1696 Space Ant --枚举,模拟,贪心,几何

题意: 有很多点,从最右下角的点开始走起,初始方向水平向右,然后以后每步只能向左边走,问最多能走多少个点. 解法: 贪心的搞的话,肯定每次选左边的与它夹角最小的点,然后走过去. 然后就是相当于模拟地去选点,然后计数,然后走过去.这题就这么搞定了. 我这里用了set和vector. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include &

POJ 1696 Space Ant 【极角排序】

题意:平面上有n个点,一只蚂蚁从最左下角的点出发,只能往逆时针方向走,走过的路线不能交叉,问最多能经过多少个点. 思路:每次都尽量往最外边走,每选取一个点后对剩余的点进行极角排序.(n个点必定能走完,这是凸包的性质决定的) #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std;

POJ 1696 Space Ant 计算几何 叉积的应用

题目大意:平面内有一些点,我们要通过一些方式来走遍这所有的点,要求一个点只能走一次,只能向左转而不能向右转.求遍历这些点的顺序. 思路:数据范围是可以怎么搞都0ms的(n<=50,case<=100),所以只要有思路就可以了. 只能左转,想想好像有点像凸包啊.但是这个题要遍历所有的点,所以就把已经走过的点删掉,然后像凸包一样的往前走,每次找一个没走过的极角最小的点走,然后把它标记上.最后都走完就全部遍历完了. CODE: #include <cmath> #include <