Barareh on Fire(双BFS)

5431: Barareh on Fire

时间限制: 1 Sec   内存限制: 128 MB
提交: 305   解决: 56
[ 提交][ 状态][ 讨论版][命题人: 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

提示

来源

acm icpc 2017 Tehran 


这题是我讲的,当时还讲错了复杂度,应该是1e8但是没有这么极端的例子。(若看到就当没这回事吧),但是当我自己重新写的时候才发现,其实自己对BFS只会用一种写法,还有一些可以进步的地方。

题意:

问穿过火堆,从S到T的步数,若存在该路径,就输出步数。

若不可能就要输出Impossible。

题解,对火堆扩圈且更新最小值(bfs),而对于人就只管走。

但是 火堆蔓延的时间小于  人走到这点的时间,就说明不能走。


这里还有两个小技巧,第一个是方向数组,以前一直会出错,你看代码就直到我怎么写了。

第二个是 

memset(数组名,127,sizeof(数组名));    最大值: 2139062143

memset(数组名,128,sizeof(数组名));    最小值:-2139062144

还有用for循环来写BFS

#include<bits/stdc++.h>
using namespace std;
int road[105][105],fire[105][105],n,m,k;
char a[105][105];
typedef struct point{
    int x,y;
}p;
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}, {0,1},
        {1,0}
};
int main()
{
    int i,j;
    p u,v,s,f;
    while(~scanf("%d%d%d",&n,&m,&k),(n||m||k)){
        queue<p>q;
        memset(road,127,sizeof(road));
        memset(fire,127,sizeof(fire));
        for(i=0;i<n;i++)  {scanf("%s",a[i]);}
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(a[i][j]=='f'){
                    fire[i][j]=0;
                    u.x=i;
                    u.y=j;
                    q.push(u);
                }else if(a[i][j]=='s'){
                    road[i][j]=0;
                    s.x=i,s.y=j;
                }else if(a[i][j]=='t'){
                    f.x=i,f.y=j;
                }
            }
        }for(;!q.empty();q.pop()){
             u=q.front();
             for(i=0;i<8;i++){
                 v.x=u.x+dir1[i][0];
                 v.y=u.y+dir1[i][1];
                 if(0<=v.x&&v.x<n&&0<=v.y&&v.y<m){
                        if(fire[v.x][v.y]>fire[u.x][u.y]+k){
                            fire[v.x][v.y]=fire[u.x][u.y]+k;
                            q.push(v);
                        }
                 }
            }
        }for(q.push(s);!q.empty();q.pop()){
            u=q.front();
            for(i=0;i<4;i++){
                    v.x=u.x+dir2[i][0];
                    v.y=u.y+dir2[i][1];
                 if(0<=v.x&&v.x<n&&0<=v.y&&v.y<m){
                        if(road[u.x][u.y]+1>=fire[v.x][v.y])continue;
                        if(road[u.x][u.y]+1<road[v.x][v.y]){
                            road[v.x][v.y]=road[u.x][u.y]+1;
                            q.push(v);
                        }
                 }
            }
        }
        /*for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                printf("%d%c",road[i][j],j==m-1?'\n':' ');
            }
        }*/
        if(road[f.x][f.y]==2139062143){
            printf("Impossible\n");
        }else{
            printf("%d\n",road[f.x][f.y]);
        }

    }return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值