CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire
Submit Page Summary Time Limit: 3 Sec Memory Limit: 512 Mb Submitted: 102 Solved: 48
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
题解:双向bfs即可,预处理
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; struct Node{
int x,y,time;
}; queue<Node> pq;
int vis[][],fire[][];
int bfsx[]={,-,,,,,-,-};
int bfsy[]={,,,-,,-,,-};
int n,m,k,min_time;
char Map[][]; void bfsa()
{
Node now,net;
while(!pq.empty())
{
now=pq.front();
pq.pop();
for(int i=;i<;i++)
{
net.x=now.x +bfsx[i];
net.y=now.y+bfsy[i];
net.time=now.time+k;
if(net.x>=&&net.x<n&&net.y>=&&net.y<m&&!vis[net.x][net.y])
{
pq.push(net);
vis[net.x][net.y]=;
fire[net.x][net.y]=net.time;
}
}
}
} int bfsb()
{
Node now,net;
while(!pq.empty())
{
now=pq.front();
pq.pop();
for(int i=;i<;i++)
{
net.x=now.x +bfsx[i];
net.y=now.y+bfsy[i];
net.time=now.time+; if(net.time<fire[net.x][net.y]&&net.x>=&&net.x<n&&net.y>=&&net.y<m&&!vis[net.x][net.y])
{
if(Map[net.x][net.y]=='t')
return net.time;
vis[net.x][net.y]=;
pq.push(net);
}
}
}
return -;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n==&&m==&&k==) break;
int num=;
while(!pq.empty()) pq.pop();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++) scanf("%s",Map[i]); Node s,t;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(Map[i][j]=='f')
{
fire[i][j]=;
vis[i][j]=;
pq.push(Node{i,j,});
num++;
}
if(Map[i][j]=='s') s.x=i,s.y=j,s.time=;
if(Map[i][j]=='t') t.x=i,t.y=j;
}
} if(num==)
{
int sum;
sum=fabs(s.x-t.x)+fabs(s.y-t.y);
cout<<sum<<endl;
continue;
} bfsa();
memset(vis,,sizeof(vis));
while(!pq.empty()) pq.pop();
vis[s.x][s.y]=;
pq.push(s);
int temp=bfsb();
if(temp!=-) cout<<temp<<endl;
else cout<<"Impossible"<<endl;
} return ;
} /**********************************************************************
Problem: 2031
User: song_hai_lei
Language: C++
Result: AC
Time:12 ms
Memory:2256 kb
**********************************************************************/
CSUOJ2031-Barareh on Fire(双向BFS)的更多相关文章
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- CSU-2031 Barareh on Fire
CSU-2031 Barareh on Fire Description The Barareh village is on fire due to the attack of the virtual ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
随机推荐
- python Django框架正式准备工作
之前由于不太了解数据库方面的知识,但经过一段时间的web应用的开发学习,成功的用其他框架连接了数据库,并完成了相关操作,数据爬取也初识了,更了解了python这门语言的语法,但路还很长,因此现在才能正 ...
- 小白学 Python 爬虫(1):开篇
人生苦短,我用 Python 引言 各位同学大家好,好久不见(可能只有一两天没见:囧)~~~ 先讲一件事情,昨天为啥没更新. emmmmmmmmm,当然是因为加班啦,快到年底了,公司项目比较忙,最近的 ...
- Jumpserver v2.0.0 使用说明
官方文档:http://www.jumpserver.org/ — 登录脚本 — 1.1 使用paramiko原生ssh协议登录后端主机(原来版本使用pexpect模拟登录) 1.2 新增使用别名或备 ...
- 领扣(LeetCode)有效的括号 个人题解
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- 自制window下core animation引擎 - demo第二弹 - 仿QQ电脑管家加速小火箭
一年前想写一个像cocoa那样,可以方便层动画开发的引擎,写着写着又逆向它的QuartzCore.framework,也就是CoreAnimation的底层,已经大半年没有搞windows这个引擎.大 ...
- Bootstrap中手指控制轮播图切换
通过手指的滑动来控制轮播图中的图片内容的切换 // 1. 获取手指在轮播图元素上的一个滑动方向(左右) // 获取界面上的轮播图容器 var $carousels = $('.carousel'); ...
- element表格点击行即选中该行复选框
关键代码如下 <el-table ref="multipleTable" :data="tableData" highlight-current-row ...
- 原创|我是如何从零学习开发一款跨平台桌面软件的(Markdown编辑器)
原始冲动 最近一直在学习 Electron 开发桌面应用程序,目的是想做一个桌面编辑器,虽然一直在使用Typore这款神器,但无奈Typore太过国际化,在国内水土不服,无法满足我的一些需求. 比如实 ...
- C#音频截取与原文匹配2:使用ffmpeg处理音频文件
ffmpeg获取音频时间 ffmpeg转换音频格式(单声道,16000hz,16bit wav) ffmpeg截取音频 不知道是不是错觉,感觉ffmpeg比NAudio要快啊~ 那么这就是第二个版本 ...
- Java关于Resource leak: 's' is never closed的问题
Resource leak: 's' is never closed的问题 问题:在编写Java时出现了Resource leak: 's' is never closed的问题,也就是对象s下面的波 ...