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三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
随机推荐
- JAVA 调用HTTP接口POST或GET实现方式
HTTP是一个客户端和服务器端请求和应答的标准(TCP),客户端是终端用户,服务器端是网站.通过使用Web浏览器.网络爬虫或者其它的工具,客户端发起一个到服务器上指定端口(默认端口为80)的HTTP请 ...
- java编程思想第四版第十四章 类型信息总结
1. Class 对象: 所有的类都是在对其第一次使用的时候,动态加载到JVM中的.当程序创建第一个对类的静态成员的引用时,就会加载这个类.这说明构造器也是类的静态方法.即使在构造器之前并没有stat ...
- lqb 入门训练 序列求和 (PS:用长整数做数据的输入输出)
入门训练 序列求和 时间限制:1.0s 内存限制:256.0MB 问题描述 求1+2+3+...+n的值. 输入格式 输入包括一个整数n. 输出格式 输出一行,包括一个整数,表示1+2+3 ...
- VLAN实验(3)
1.选择2台S3700和5台pc机,并根据实验编址完成此拓扑图. 2.启动设备,检查设备的连通性: 由于现在我们还没有划分VLAN,这5台PC,还在同一个VLAN中,现在我们启动所有的设备,这是所有的 ...
- python:timeit模块
(鱼c)timeit模块详解——准确测量小段代码的执行时间 http://bbs.fishc.com/forum.php?mod=viewthread&tid=55593&extra= ...
- ubuntu触摸板双指滑动,页面滚动方向
setting——mouse & Touchpad——Natural scrolling 跟我的另一台本子一样了-
- IDEA+SpringBoot+Mybatis+maven分布式项目框架的搭建
参考文章:https://blog.csdn.net/qq_34410726/article/details/98214992 一.maven分布式工程的基本架构 demo #父工程模块,主要用来定 ...
- 线程中synchronized关键字和lock接口的异同
一.synchronized关键字 1.可以用来修饰代码块 synchronized (this) { // 同步的关键字 this 表示当前线程对象 if (num == 0) { break; } ...
- Handler+Looper+MessageQueue深入详解
概述:Android中的异步处理机制由四部分组成:Handler+Looper+MessageQueue+message,用于实现线程间的通信. 用到的概念: Handler: 主要作用是发送消息和处 ...
- 解决无法定位软件包 或 install net-tools
解决无法定位软件包 或 install net-tools 当我们安装好Linux后,因为里面有很多功能服务没有安装(如ifconfig.vsftpd) 所以出现一些command '**** ...