POJ 1566(toj 1952)

本文介绍了一个利用最短路径算法解决特定几何问题的方法。该问题涉及在一个包含障碍物的房间中寻找两点间最短路径,通过将几何元素转化为图论中的节点和边,并运用最短路径算法求解。文章提供了详细的实现代码及思路解析。
The Doors
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 126    Accepted Runs: 69



The Problem

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 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


Source: Mid-Central USA 1996

交题的时候poj挂了,在hdu上也没找到,突然想起自己学校的oj,在toj上一找还真找到了,交了一发,1A,开心~

说明toj还是很厉害的,题目还是很全的~

思路:乍一看感觉是一道几何题,再一看发现可以用最短路做

在所有点之间连线,判断跟墙是否相交,不相交的话就建边,然后跑一边最短路,什么方法都行了

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <iomanip>

#define INF 100000000.0

using namespace std;

struct point
{
    double x,y;
    point(){};
    point (double _x,double _y)
    {
        x=_x,y=_y;
    }
};
typedef point vec;

double operator /(vec a, vec b)
{
    return (a.x*b.y-a.y*b.x);
}
vec operator- (point a,point b)
{
    vec c;
    c.x=a.x-b.x;
    c.y=a.y-b.y;
    return c;
}
struct line
{
    point a,b;
    line(){};
    line (point _a,point _b)
    {
        a=_a;
        b=_b;
    }
};
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

struct edge
{
    int v,next;
    double w;
};
int cross(line m,line n)
{
    if(((n.a-m.a)/(m.b-m.a))*((n.b-m.a)/(m.b-m.a))<=0
       &&((m.a-n.a)/(n.b-n.a))*((m.b-n.a)/(n.b-n.a))<=0)
       return 1;
       else return 0;
}
edge e[5005];
int head[105],cnt=0,vis[5005],n,m;
double dist[105];
queue<int>q;
void addedge(int u,int v,double w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void spfa(int s)
{
    for(int i=0;i<=n;i++)
        dist[i]=INF,vis[i]=0;
    while(!q.empty()) q.pop();
    q.push(s);
    vis[s]=1;
    dist[s]=0.0;
    while(!q.empty())
    {
        int tmp;
        tmp=q.front();
        q.pop();
        for(int i=head[tmp];i!=-1;i=e[i].next)
        {
            if(dist[e[i].v]>dist[tmp]+e[i].w)
            {
                dist[e[i].v]=dist[tmp]+e[i].w;
                if(!vis[e[i].v])
                {
                    vis[e[i].v]=1;
                    q.push(e[i].v);
                }
            }

        }
        vis[tmp]=0;
    }
}
point p[105];
line le[5005];
bool check(int i,int j)
{
    line temp(p[i],p[j]);
    int left,right;
    if(i!=0 && j!=n-1)
    {
        left=(((i-1)/4)+1)*3;
        right=((j-1)/4)*3;
        for(int c1=left;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i==0 && j!=n-1)
    {
        right=((j-1)/4)*3;
        for(int c1=0;c1<right;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else if(i!=0 && j==n-1)
    {
        left=(((i-1)/4)+1)*3;
        for(int c1=left;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    else
    {
        for(int c1=0;c1<m-1;c1++)
        {
            if(cross(temp,le[c1]))
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int t;
    while(cin>>t)
    {
        if(t==-1)
            break;
        init();
        int totl=0,totp=1;
        n=4*t+2;
        m=3*t;
        p[0].x=0,p[0].y=5.0;
        p[n-1].x=10.0,p[n-1].y=5.0;
        for(int i=0;i<t;i++)
        {
            double x,y1,y2,y3,y4;
            point a,b;
            line tmp;
            cin>>x>>y1>>y2>>y3>>y4;
            p[totp].x=x,p[totp++].y=y1;
            p[totp].x=x,p[totp++].y=y2;
            p[totp].x=x,p[totp++].y=y3;
            p[totp].x=x,p[totp++].y=y4;
            a.x=x,a.y=0.0;
            b.x=x,b.y=y1;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y2;
            b.x=x,b.y=y3;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
            a.x=x,a.y=y4;
            b.x=x,b.y=10.0;
            tmp.a=a,tmp.b=b;
            le[totl++]=tmp;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(check(i,j))
                    addedge(i,j,dis(p[i],p[j]));
            }
        }
        spfa(0);
        double ans=dist[n-1];
        cout<<fixed<<setprecision(2)<<ans<<endl;
    }
    return 0;
}


本研究基于扩展卡尔曼滤波(EKF)方法,构建了一套用于航天器姿态与轨道协同控制的仿真系统。该系统采用参数化编程设计,具备清晰的逻辑结构和详细的代码注释,便于用户根据具体需求调整参数。所提供的案例数据可直接在MATLAB环境中运行,无需额外预处理步骤,适用于计算机科学、电子信息工程及数学等相关专业学生的课程设计、综合实践或毕业课题。 在航天工程实践中,精确的姿态与轨道控制是保障深空探测、卫星组网及空间设施建设等任务成功实施的基础。扩展卡尔曼滤波作为一种适用于非线性动态系统的状态估计算法,能够有效处理系统模型中的不确定性与测量噪声,因此在航天器耦合控制领域具有重要应用价值。本研究实现的系统通过模块化设计,支持用户针对不同航天器平台或任务场景进行灵活配置,例如卫星轨道维持、飞行器交会对接或地外天体定点着陆等控制问题。 为提升系统的易用性与教学适用性,代码中关键算法步骤均附有说明性注释,有助于用户理解滤波器的初始化、状态预测、观测更新等核心流程。同时,系统兼容多个MATLAB版本(包括2014a、2019b及2024b),可适应不同的软件环境。通过实际操作该仿真系统,学生不仅能够深化对航天动力学与控制理论的认识,还可培养工程编程能力与实际问题分析技能,为后续从事相关技术研究或工程开发奠定基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值