UVA - 11624 Fire!

本文介绍了一个迷宫逃生问题的解决方案,使用广度优先搜索算法帮助人物Joe从着火的迷宫中安全逃脱。通过记录位置、时间和区分火势与人物状态,确保找到最短逃生路径。

Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input

#

#JF#
#..#
#..#
3 3

#

J.

#.F
Sample Output
3
IMPOSSIBLE
一个迷宫着火了,里面有一个人,火和人都会往上下左右四个方向移动,有墙的地方和火经过的地方人都不能过。
先将所有的起火点放入队列,然后再把人的位置放入队列。用结构体记录位置,时间和区分火和人。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
    int x,y,time,id;
};

int n,m;
int Map[1001][1001];
char ch[1001];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
queue<node> q;

int bfs()
{
    node a,b;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            b.time=a.time+1;
            b.id=a.id;
            if(b.id&&(b.x==-1||b.x==n||b.y==-1||b.y==m))//判断人走出迷宫的条件
            {
                return b.time;
            }
            if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&!Map[b.x][b.y])//火和人到达的地方
            {
                Map[b.x][b.y]=1;
                q.push(b);
            }
        }
    }
    return -1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        node s1,s2;
        while(!q.empty())q.pop();//全局变量,一定记得清空队列
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",ch);
            for(int j=0;j<m;j++)
            {
                if(ch[j]=='.')
                {
                    Map[i][j]=0;
                }
                else if(ch[j]=='#')
                {
                    Map[i][j]=1;
                }
                else if(ch[j]=='F')
                {
                    Map[i][j]=1;
                    s1.x=i,s1.y=j,s1.time=0,s1.id=0;
                    q.push(s1);//将火的信息加入队列
                }
                else if(ch[j]=='J')
                {
                    Map[i][j]=1;
                    s2.x=i,s2.y=j,s2.time=0,s2.id=1;
                }
            }
        }
        q.push(s2);//最后将人的信息加入队列
        int ans=bfs();
        if(ans!=-1)
        {
            printf("%d\n",ans);
        }
        else
        {
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}
【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值