poj1556 The Doors

本文介绍了一种求解迷宫最短路径的算法实现,通过输入不同数量和位置的障碍墙来寻找从起点到终点的最短路径。利用叉乘判断相交的方法解决了路径规划问题,并提供了一个具体的示例。

http://www.elijahqi.win/archives/1117

Description
You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

Input

The input data for the illustrated chamber would appear as follows.

2
4 2 7 8 9
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output
The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

这里用了叉乘判断相交的解法,本来死活过不去,后来随便改改等于号什么的就过了。。对这个叉乘什么的还不太会,留坑待填,另外%lf 在Poj上g++会有问题

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 110
using namespace std;
struct node{
    int y,next;double z;
}data[55000];
struct pp
{
    double x,y;
}point[110];
struct lli
{
    pp pa,pb;
}line[110];
int h[N<<2],num,cnt,m;
inline void cl(double x1,double y1,double x2,double y2){line[++m].pa.x=x1;line[m].pa.y=y1;line[m].pb.x=x2;line[m].pb.y=y2;}
inline double calc(pp a,pp b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline void insert1(int x,int y,double z){
    data[++cnt].y=y;data[cnt].next=h[x];data[cnt].z=z;h[x]=cnt;
    data[++cnt].y=x;data[cnt].next=h[y];data[cnt].z=z;h[y]=cnt;
}
inline double multiply(pp a,pp b,pp c){
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
inline bool cross(lli line1,lli line2){
    pp aa=line1.pa,bb=line1.pb,cc=line2.pa,dd=line2.pb;
    if (max(aa.x,bb.x)<min(cc.x,dd.x)) return false;
    if (max(aa.y,bb.y)<min(cc.y,dd.y)) return false;
    if (max(cc.x,dd.x)<min(aa.x,bb.x)) return false;
    if (max(cc.y,dd.y)<min(aa.y,bb.y)) return false;
    if (multiply(cc,bb,aa)*multiply(bb,dd,aa)<=0) return false;
    if (multiply(aa,dd,cc)*multiply(dd,bb,cc)<=0) return false;
    return true;
}
inline bool judge(pp a,pp b){
    lli line1;line1.pa=a;line1.pb=b;
    for (int i=1;i<=m;++i)
        if (cross(line[i],line1)) return false;
    return true;
}
bool flag[N<<2];
double f[N<<2]; 
inline void spfa(){
    queue<int> q;memset(flag,0,sizeof(flag));flag[1]=true;q.push(1);memset(f,0x7f,sizeof(f));f[1]=0;
    while (!q.empty()){
        int x=q.front();q.pop();flag[x]=false;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;double z=data[i].z;
            if (f[x]+z<f[y]) {
                f[y]=f[x]+z;if (flag[y]==false){
                    q.push(y);flag[y]=true;
                }
            }
        }
    }
}
int n;double x,y_1,y_2,y_3,y_4;
int main(){
    freopen("poj1556.in","r",stdin);
    while (1){
        scanf("%d",&n);if (n==-1) break;
        memset(h,0,sizeof(h));m=num=cnt=0;
        point[++num].x=0;point[num].y=5;
        for (int i=1;i<=n;++i){
            scanf("%lf%lf%lf%lf%lf",&x,&y_1,&y_2,&y_3,&y_4);
            point[++num].x=x;point[num].y=y_1;point[++num].x=x;point[num].y=y_2;
            point[++num].x=x;point[num].y=y_3;point[++num].x=x;point[num].y=y_4;
            cl(x,0,x,y_1);cl(x,y_2,x,y_3);cl(x,y_4,x,10);
        }point[++num].x=10;point[num].y=5;
        //for (int i=1;i<=m;++i) printf("%lf %lf %lf %lf\n",line[i].pa.x,line[i].pa.y,line[i].pb.x,line[i].pb.y);
        //for (int i=1;i<=num;++i) printf("%lf %lf\n",point[i].x,point[i].y);
        for (int i=1;i<=num;++i)
            for (int j=i+1;j<=num;++j){
                if (judge(point[i],point[j])) insert1(i,j,calc(point[i],point[j]));
            }
        //for (int i=1;i<=cnt;++i) printf("%d %lf\n",data[i].y,data[i].z);
        spfa();
        printf("%.2lf\n",f[num]);
    }
    return 0;
}

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值