Barareh on Fire(两次bfs,动态地图)

本文介绍了一种解决火势蔓延环境下寻找最短安全路径的问题。通过两次BFS算法实现,首次预处理火势蔓延状态,再次确定逃生路径。特别关注了动态地图环境下算法的设计与实现。

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

题目描述

The Barareh village is on fire due to the attack of the virtual enemy. Several places are already on fire and the fire is spreading fast to other places. Khorzookhan who is the only person remaining alive in the war with the virtual enemy,tries to rescue himself by reaching to the only helicopter in the Barareh villiage.
Suppose the Barareh village is represented by an n m grid. At the initial time, some grid cells are on fire. If a cell catches fire at time x, all its 8 vertex-neighboring cells will catch fire at time x + k. If a cell catches fire, it will be on fire forever.
At the initial time, Khorzookhan stands at cell s and the helicopter is located at cell t. At any time x, Khorzookhan can move from its current cell to one of four edge-neighboring cells, located at the left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1. Note that each move takes one second.
Your task is to write a program to find the shortest path from s to t avoiding fire.

输入

There are multiple test cases in the input. The first line of each test case contains three positive integers n, m and k (1 ⩽ n, m, k ⩽ 100), where n and m indicate the size of the test case grid n m, and k denotes the growth rate of fire. The next n lines, each contains a string of length m, where the jth character of the ith line represents the cell (i, j) of the grid. Cells which are on fire at time 0, are presented by character “f”. There may exist no “f” in the test case. 
The helicopter and Khorzookhan are located at cells presented by “t” and “s”, respectively. Other cells are filled by “-” characters. The input terminates with a line containing “0 0 0” which should not be processed.

输出

For each test case, output a line containing the shortest time to reach t from s avoiding fire. If it is impossible to reach t from s, write “Impossible” in the output.

样例输入
7 7 2
f------
-f---f-
----f--
-------
------f
---s---
t----f-
3 4 1
t--f
--s-
----
2 2 1
st
f-
2 2 2
st
f-
0 0 0

样例输出
4
Impossible
Impossible
1

题意:刚开始地图上有一些地方已经着火,用'f'表示,初始的时候人在's'处,想要到't'处坐直升机逃跑。火势会蔓延,每过k秒大火会蔓延到周围的八个网格,人每一秒只能选择上下左右四个方向中的一个方向移动。问是否能成功逃生,如果能最短时间是多少。

这个题目与以往的BFS题目不同,他的地图是动态的(火势是蔓延的),所以先BFS一次确定一下火势蔓延的状态(预处理火势蔓延的地图),然后再BFS一次确定逃跑路线。第一次BFS记录下每一个网格被火势蔓延的时间time,在第二次bfs的时候只需要判断一下人走到某一个网格的时间是否小于time即可。



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
struct node
{
    int x;
    int y;
};
int dir1[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int dir2[4][2]={-1,0,0,1,1,0,0,-1};
int vis[105][105];
int dp[105][105];
char maze[105][105];
int main()
{
    int n,m,k;
    node u,v,a,b;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        if(n==0 && m==0 && k==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%s",&maze[i]);
        queue<node> q;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                vis[i][j]=inf;
                dp[i][j]=inf;
                if(maze[i][j]=='f')
                {
                    vis[i][j]=0;
                    u.x=i;
                    u.y=j;
                    q.push(u);
                }
                if(maze[i][j]=='s')
                {
                    dp[i][j]=0;
                    a.x=i;
                    a.y=j;
                }
                if(maze[i][j]=='t')
                {
                    b.x=i;
                    b.y=j;
                }

            }
        }
        while(!q.empty())
        {
            u=q.front();
            for(int i=0;i<8;i++)
            {
                v.x=u.x+dir1[i][0];
                v.y=u.y+dir1[i][1];
                if(v.x>=0 && v.x<n && v.y>=0 && v.y<m)
                {
                    if(vis[v.x][v.y]>vis[u.x][u.y]+k)
                    {
                        vis[v.x][v.y]=vis[u.x][u.y]+k;
                        q.push(v);
                    }
                }
            }
            q.pop();

        }
        q.push(a);
        while(!q.empty())
        {
            u=q.front();
            for(int i=0;i<4;i++)
            {
                v.x=u.x+dir2[i][0];
                v.y=u.y+dir2[i][1];
                if(v.x>=0 && v.x<n && v.y>=0 && v.y<m)
                {
                   if(dp[u.x][u.y]+1<vis[v.x][v.y] && dp[u.x][u.y]+1<dp[v.x][v.y])
                   {
                       dp[v.x][v.y]=dp[u.x][u.y]+1;
                       q.push(v);
                   }

                }
            }
            q.pop();
        }

        if(dp[b.x][b.y]==inf)
           printf("Impossible\n");
        else
            printf("%d\n",dp[b.x][b.y]);

    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值