传送门:

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

4
Impossible
Impossible
1

Hint

Source

ATRC2017

分析:刚开始地图上有一些地方已经着火, 用'f'表示,初始的时候人在's'处,想要到't'处坐直升机逃跑。火势会蔓延,每过k秒大火

会蔓延到周围的八个网格,人每一秒只能选择上下左右四个方向中的一个方向移动。问是否能成功逃生,如果能最短时间是多少。

思路:这道题我们可以把火看成是墙壁, 和普通的bfs题不一样,这个“墙壁”会变化扩散。所以我们可以先bfs一次确定火势蔓延的

状态, 然后再bfs一次确定逃跑路线。第一次bfs记录下每一个网格被火势蔓延的时间time,在第二次bfs的时候只需要判断一下人走

到某一个网格的时间是否小于time即可。

ps:火每k秒往8个方向扩展一次
   人只有四个方向

具体做法:

两遍bfs,第一遍预处理每个点火烧到的时间,第二遍判断人是否能安全逃生
 code:

#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 105
struct node
{
int x,y,time;
};
int vis1[max_v][max_v];//火烧到没有
int vis2[max_v][max_v];//人走过没有
int tim[max_v][max_v];//每个点火烧到的时间
int dir[][]={,,,,-,,,-,,,-,,,-,-,-};
char G[max_v][max_v];
int n,m,k,mintime;//每k秒火向8个方向扩展一次
queue<node> q;
bool f(int x,int y)//有没有越界
{
if(x>=&&x<n&&y>=&&y<m)
return true;
return false;
}
void init()//初始化
{
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
while(!q.empty())
{
q.pop();
}
}
void bfs1()//火 主要是得到火烧到每个点的时间
{
node p,next;
while(!q.empty())
{
p=q.front();
q.pop(); for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][];
next.time=p.time+k;//注意理解 if(f(next.x,next.y)&&vis1[next.x][next.y]==)//火没有越界且没有烧到
{
vis1[next.x][next.y]=;
tim[next.x][next.y]=next.time;
q.push(next);
}
}
}
}
bool bfs2()//人
{
node p,next;
while(!q.empty())
{
p=q.front();
q.pop(); if(G[p.x][p.y]=='t')
{
mintime=p.time;
return true;
}
for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][];
next.time=p.time+;//人每走一步时间加1 if(f(next.x,next.y)&&next.time<tim[next.x][next.y]&&vis2[next.x][next.y]==)//没有越界,当前时间小于火烧到时间 ,点人没有走过
{
vis2[next.x][next.y]=;
q.push(next);
}
}
}
return false;
}
int main()
{
node temp;
while(cin>>n>>m>>k,n&&m&&k)
{
int num=;
init();
node s,f;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>G[i][j];
if(G[i][j]=='f')
{
tim[i][j]=;
vis1[i][j]=;
temp.x=i;
temp.y=j;
temp.time=; q.push(temp);//将所有火入队,得到每个点火烧到的时间
num++;//火的个数
}
if(G[i][j]=='s')
{
s.x=i;
s.y=j;
s.time=;
vis2[s.x][s.y]=;//人的起点
}
if(G[i][j]=='t')
{
f.x=i;//人的终点
f.y=j;
}
}
}
if(num==)//没有火!!!
{
mintime=abs(s.x-f.x)+abs(s.y-f.y);
cout<<mintime<<endl;
continue;
} bfs1();//得到每个点的起火时间 while(!q.empty()) q.pop(); q.push(s);//人的起点入队
if(bfs2())
{
cout<<mintime<<endl;
}
else
{
cout<<"Impossible"<<endl;
}
}
return ;
}

CSU - 2031 Barareh on Fire (两层bfs)的更多相关文章

  1. CSUOJ 2031 Barareh on Fire

    Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...

  2. CSU 2031

    2031: Barareh on Fire Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitt ...

  3. HDU 2102 A计划(两层地图加时间限制加传送门的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others)    Me ...

  4. Fire! UVA - 11624 (两步bfs)

    题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...

  5. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. CSU-2031 Barareh on Fire

    CSU-2031 Barareh on Fire Description The Barareh village is on fire due to the attack of the virtual ...

  7. Deep Learning入门视频(上)_一层/两层神经网络code

    关于在51CTO上的深度学习入门课程视频(9)中的code进行解释与总结: (1)单层神经网络: #coding:cp936 #建立单层神经网络,训练四个样本, import numpy as np ...

  8. Linux网络栈下两层实现

    http://www.cnblogs.com/zmkeil/archive/2013/04/18/3029339.html 1.1简介 VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linu ...

  9. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

随机推荐

  1. Hql语句转化为sql语句中文乱码问题

    刚刚学习Hql语句就出现这一的问题,百度半天终于解决了,总结一下解决的方案: 出现中文乱码最可能的原因是hibernate配置文件配置的问题 1.检查url路径是否指定字符集为UTF-8 <pr ...

  2. asp.net 日期转换为大写汉字

    //年份转换为大写汉字 public static string numtoUpper(int num) { return "零壹贰叁肆伍陆柒捌玖"[num].ToString() ...

  3. react+javascript前端进阶

    组合1: react技术栈(react(阮一峰react入门,官网教程).redux(阮一峰redux入门,官网教程).saga)+JS(ES6)+antd+you don`t know JS(上中下 ...

  4. mac下打开hosts文件

    1打开控制台 输入vi(空格)/etc/hosts 进入hosts文件,输入i更改为编辑状态,更改完esc然后shift+:在输入wq保存退出 2打开Finder然后选择上面前往,到前往文件夹,输入/ ...

  5. 按键精灵Q语言基础

    一.数据类型1.1数据类型可以表示一切的类型variant逻辑类型:boolean (true,false)数学类型: 整数:byte(0-255),integer(-32768-32767),lon ...

  6. python 进程池的使用

    进程同步 进程的数据是独立存在的,进程也能加锁. from multiprocessing import Process, Lock def f(l,i): l.acquire() print('he ...

  7. IIS6.0配置正常,但是显示“网页无法访问”,Httperr.log中显示全是“Connections_refused”,问题总结

    转自:http://blog.csdn.net/foxeatapple/article/details/21983869 最近部门的Web服务器突然无法访问! 加班解决! 问题症状: 1.“Inter ...

  8. javax.swing.JComponent 调用顺序

    网上截取的,感觉挺有用,记录下来. http://bbs.csdn.net/topics/310041707 java swing 感觉好复杂啊…………一点都不想用但是作业要用到 >_<; ...

  9. python 二叉树计算器

    例子:计算1+2+3+4的值 代码: class Buffer(object): """字符串处理函数""" def __init__(se ...

  10. JS复制DOM元素文字内容

    要实现的效果:将HTML页面中的某个DOM元素例如DIV下面的文本内容进行复制. 实现过程如下: <html> <head> <title>Copy text De ...