POJ 1696 Space Ant(卷包裹 计算几何)

本文介绍了一个简单的路径规划问题,目标是让一只蚂蚁走过所有的点,且只能向左转。通过不断寻找与当前路径夹角最小的下一个点来完成路径规划。使用了基本的几何计算方法实现这一目标。

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

题目链接:http://poj.org/problem?id=1696

一只蚂蚁,只能往左拐,求一条路线能让蚂蚁走过所有的点

题目比较简单,每次找最外面一层的,可以直接凸包,每次求凸包

我是直接每次找和上一条路线夹角最小的为下一次的目标点,直到走完所有点!



#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
#define maxn 100
struct point{
    double x,y;
    bool is_use;
    int num;
}po[maxn],temp,now;
double angle(point a, point b, point c){
    c.x-=(b.x-a.x);
    c.y-=(b.y-a.y);
  double ux = b.x - a.x, uy = b.y - a.y;
  double vx = c.x - a.x, vy = c.y - a.y;
  return acos((ux*vx + uy*vy) /
              sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));
}
int n,pos;
int main(){
    int i,j,k,t;
    double ang,re;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        printf("%d ",n);
        temp.y=100000;
        for(i=0;i<n;i++){
            scanf("%d%lf%lf",&po[i].num,&po[i].x,&po[i].y);
            if(po[i].y < temp.y) pos=i,temp=po[i];
            po[i].is_use=true;
        }
        printf("%d ",po[pos].num);
        po[pos].is_use=false;
        temp.x-=10;
        now=po[pos];
        for(i=0;i<n-1;i++){
            ang=3.14;
            for(j=0;j<n;j++){
                if(po[j].is_use==false) continue;
                re=angle(temp,now,po[j]);
                if(re<ang){
                    pos=j;
                    ang=re;
                }
            }
            printf("%d ",po[pos].num);
            po[pos].is_use=false;
            temp=now;
            now=po[pos];
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值