Barareh on Fire

本文探讨了在一个由网格组成的地图上,面对火灾蔓延的情况,如何寻找一条从起点到终点的最短路径。火灾以固定的速率向周围八个方向扩散,而行动者只能向四个基本方向移动,且必须避开火势。

5431: Barareh on Fire

时间限制: 1 Sec   内存限制: 128 MB
提交: 306   解决: 57
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述   点击打开链接

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

题意:给定n行m列的字符数组,其中'f'表示火,'s'表示初始位置,'t'表示目标位置,'k'表示每经过k秒 周围的八个格子都变为火,求一个人从s位置到t位置的最短时间(他只能沿上下左右四个方向移动),若不能到达输出Impossible

思路:预处理每个格子起火的时间,在bfs扩展结点的时候加上判断,当达到新节点的时候该格子没着火才把新结点加进去,最后由于着火点不止一个,所以初始队列时要把所有着火点放进去

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
typedef struct node
{
    int x,y;
}NODE;
int dir[8][2]={
-1,-1,-1,0,-1,1,
0,-1,0,1,
1,-1,1,0,1,1
},f[4][2]={-1,0,0,-1,0,1,1,0};   //人和火的方向数组
queue<NODE> q;
int main()
{
    char s[110][110];
    int n,m,i,j,k,dp[110][110],a[110][110];//dp[i][j]记录到i行j列这个位置所需的最短时间
    NODE p,u,v,aa,b;                       //a[i][j]记录i,j位置着火的时间
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        if(!m&&!n&&!k)
            break;
        for(i=0;i<n;i++)
            scanf("%s",s[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                dp[i][j]=10000;//n,m最大为100,所以最长时间不超过10000
                a[i][j]=10000;
                if(s[i][j]=='f')
                {
                    p.x=i;
                    p.y=j;
                    a[i][j]=0;
                    q.push(p);
                }
                if(s[i][j]=='s')
                {
                    dp[i][j]=0;
                    aa.x=i;
                    aa.y=j;
                }
                if(s[i][j]=='t')
                {
                    b.x=i;
                    b.y=j;
                }
            }
        }
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            for(i=0;i<8;i++)
            {
                v.x=u.x+dir[i][0];
                v.y=u.y+dir[i][1];
                if(v.x>=0&&v.x<n&&v.y>=0&&v.y<m)
                {
                    if(a[u.x][u.y]+k<a[v.x][v.y])
                    {
                        a[v.x][v.y]=a[u.x][u.y]+k;
                        q.push(v);
                    }
                }
            }
        }
        q.push(aa);
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            for(i=0;i<4;i++)
            {
                v.x=u.x+f[i][0];
                v.y=u.y+f[i][1];
                if(v.x>=0&&v.x<n&&v.y>=0&&v.y<m)
                {
                    if(dp[u.x][u.y]+1>=a[v.x][v.y])
                        continue;
                    if(dp[v.x][v.y]>dp[u.x][u.y]+1)
                    {
                        dp[v.x][v.y]=dp[u.x][u.y]+1;
                        q.push(v);
                    }
                }
            }
        }
        if(dp[b.x][b.y]==10000)
        {
            printf("Impossible\n");
        }
        else
            printf("%d\n",dp[b.x][b.y]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2020/3/16

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值