Space Ant - POJ 1696 (凸包)

题目大意:给一些散列点然后初始点是坐标最下面最左面的点,然后只能往左走,求出来最多可以经过多少个点,把序号输出出来。

分析:先求出来初始的点,然后不断排序找出来最近的凸点....复杂度是 n^2*log(n)。。。。不多点很少,所以随意玩。

代码如下:

=====================================================================================================================

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;

const int MAXN = 107;
const double EPS = 1e-8;

struct Point
{
    double x, y;
    int id;
    Point(double x=0, double y=0):x(x),y(y){}
    Point operator - (const Point &tmp) const{
        return Point(x-tmp.x, y-tmp.y);
    }
    double operator *(const Point &tmp) const{
        return x*tmp.x + y*tmp.y;
    }
    double operator ^(const Point &tmp) const{
        return x*tmp.y - y*tmp.x;
    }
};
double Dist(Point a, Point b)
{
    return sqrt((a-b)*(a-b));
}
Point p[MAXN];
int ki;

bool cmp(Point a, Point b)
{
    double t = (a-p[ki]) ^ (b-p[ki]);

    if(fabs(t) < EPS)
        return Dist(p[ki], a) < Dist(p[ki], b);
    return t > EPS;
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int i, N;

        scanf("%d", &N);

        for(int i=0; i<N; i++)
        {
            scanf("%d%lf%lf", &p[i].id, &p[i].x, &p[i].y);
            if(p[i].y < p[0].y || (p[i].y==p[0].y && p[i].x < p[0].x))
                swap(p[i], p[0]);
        }

        ki = 0;

        for(i=1; i<N; i++, ki++)
        {
            sort(p+i, p+N, cmp);
        }

        printf("%d", N);
        for(i=0; i<N; i++)
            printf(" %d", p[i].id);
        printf("\n");
    }

    return 0;
}
时间: 2024-10-11 14:07:22

Space Ant - POJ 1696 (凸包)的相关文章

POJ 1696 /// 凸包

题目大意: 不能向左拐 不能重复走 就是求一个螺旋凸包 把已经是凸包内的点标记一下就行 因为凸包的性质 所有点都能走到 注意起点的选择 还有 反复求凸包的过程中边界的改变 #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> using namespace std; const int N=55; const double eps=1e-10; double a

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: 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(极角排序)

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 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3967   Accepted: 2489 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

POJ1696 Space Ant(贪心、向量叉乘、向量极角、线段相交、点在线段上)

题目链接: http://poj.org/problem?id=1696 题目描述: Space Ant 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 Y1999 and called it M11. It has only one ey