POJ1696 Space Ant【计算几何】

本文介绍了一个有趣的空间路径规划问题——空间蚂蚁(M11)寻找食物的最优路径。通过两种算法解决这一挑战,确保蚂蚁能够遍历最多的点并遵循特定的行走规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Space Ant

Time Limit: 1000MS Memory Limit: 10000K

Problem 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 eye on the left side of its head and just three feet all on the right side of its body and suffers from three walking limitations:

  1. It can not turn right due to its special body structure.
  2. It leaves a red path while walking.
  3. It hates to pass over a previously red colored path, and never does that.

The pictures transmitted by the Discovery space ship depicts that plants in the Y1999 grow in special points on the planet. Analysis of several thousands of the pictures have resulted in discovering a magic coordinate system governing the grow points of the plants. In this coordinate system with x and y axes, no two plants share the same x or y.
An M11 needs to eat exactly one plant in each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end of the day. Notice that it can reach a plant in any distance.
The problem is to find a path for an M11 to let it live longest.
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (xA, yA) is the plant with the least y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that the solution path should not cross itself and all of the turns should be counter-clockwise. Also note that the solution may visit more than two plants located on a same straight line.
这里写图片描述

Input

The first line of the input is M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line is N, the number of plants in that test case (1 <= N <= 50), followed by N lines for each plant data. Each plant data consists of three integers: the first number is the unique plant index (1..N), followed by two positive integers x and y representing the coordinates of the plant. Plants are sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates are at most 100.

Output

Output should have one separate line for the solution of each test case. A solution is the number of plants on the solution path, followed by the indices of visiting plants in the path in the order of their visits.

Examples

Input
2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16

Output
10 8 7 3 4 9 5 6 2 1 10
14 9 10 11 5 12 8 7 6 13 4 14 1 3 2


【题目链接】 Space Ant

【题意】

有一只蚂蚁,只会向左转(也可以直着走),现在给出平面上很多个点,求解一种走法,能使得蚂蚁能经过的点最多,每个顶点该蚂蚁只能经过一次,且所行走的路线不能发生交叉

【思路】

首先显然,一定有方法可以遍历所有的点,只要按照螺旋线类似的轨迹向里面转即可。

方法一:

先确定最下面(左下)的点为起始点,然后不断地进行极角排序(因为是内旋类似于求凸包,所以叉积小的优先),找到内旋角度最小的那个点,再以这个点为中心点对剩下的点进行极角排序,以此类推。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 55;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n;

struct node
{
    double x,y;
    int pos;
    node(double x=0,double y=0,int pos=0):x(x),y(y),pos(pos) {}
}a[maxn],tu[maxn],base;

double getlen(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double cross(node a,node b,node c)   //a为顶点 ,叉积
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

int kk;

bool cmp(node A,node b)
{
    if(cross(a[kk],A,b)==0)
        return getlen(a[kk],A)<getlen(a[kk],b);
    return cross(a[kk],A,b)>0;
}


int main()
{
    rush()
    {
        scanf("%d",&n);
        base={INF,INF};
        int Min=0;
        for(int i=0;i<n;i++)
        {
            double x;
            scanf("%lf%lf%lf",&x,&a[i].x,&a[i].y);
            a[i].pos=i+1;
            if(a[i].y<base.y||(a[i].y==base.y&&a[i].x<base.x))
            {
                base.x=a[i].x;
                base.y=a[i].y;
                Min=i;
            }
        }
        kk=0;
        swap(a[Min],a[0]);
        sort(a+1,a+n,cmp);
        printf("%d",n);
        printf(" %d",a[0].pos);
        for(int i=1;i<n-1;i++)
        {
            kk=i;
            sort(a+i,a+n,cmp);
            printf(" %d",a[i].pos);
        }
        printf(" %d\n",a[n-1].pos);
    }
}

方法二:

从网上其他类似的题目改过来的,自己还不是很理解,先存着……

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 55;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n;
int top;
int vis[maxn];

struct node
{
    double x,y;
    int pos;
    node(double x=0,double y=0,int pos=0):x(x),y(y),pos(pos) {}
}a[maxn],tu[maxn],base;

double getlen(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double cross(node a,node b,node c)   //a为顶点 ,叉积
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

bool cmp(node a,node b)
{
    if(cross(base,a,b)==0)
        return getlen(base,a)<getlen(base,b);
    return cross(base,a,b)>0;
}

void get_tubao()
{
   top=0;
   while(top<n)
   {
       for(int i=0;i<n;i++)
       {
           if(vis[a[i].pos]) continue;
           while(top>1&&cross(tu[top-2],tu[top-1],a[i])<=0)
           {
               top--;
               vis[tu[top].pos]=0;
           }
           tu[top++]=a[i];
           vis[a[i].pos]=1;
       }
       int mm=top;
       for(int i=n-1;i>=0;i--)
       {
           if(vis[a[i].pos]) continue;
           while(top>mm&&cross(tu[top-2],tu[top-1],a[i])<=0)
           {
               top--;
               vis[tu[top].pos]=0;
           }
           tu[top++]=a[i];
           vis[a[i].pos]=1;
       }
   }
}

int main()
{
    rush()
    {
        scanf("%d",&n);
        base={INF,INF};
        int Min=0;
        mst(vis,0);
        for(int i=0;i<n;i++)
        {
            double x;
            scanf("%lf%lf%lf",&x,&a[i].x,&a[i].y);
            a[i].pos=i+1;
            if(a[i].y<base.y||(a[i].y==base.y&&a[i].x<base.x))
            {
                base.x=a[i].x;
                base.y=a[i].y;
                Min=i;
            }
        }
        swap(a[Min],a[0]);
        sort(a+1,a+n,cmp);
        get_tubao();
        printf("%d",n);
        for(int i=0;i<top;i++)
        {
            printf(" %d",tu[i].pos);
        }
        puts("");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值