UVA - 11624 Fire! 双向BFS追击问题
Fire!
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
双向BFS。这题会被样例误导比较坑。。F点其实可以有多个。把J和someF分别加入两个队列,先扩展F点,把一步之内的点标记下,再扩展J,使得F可以影响J的路线。当J到达边界即逃脱,否则被#墙及F点围堵Fire。。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,s;
}node; int main()
{
int tt,n,m,i,j;
scanf("%d",&tt);
while(tt--){
scanf("%d%d",&n,&m);
queue<Node> qj,qf;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='J'){
b[i][j]=;
node.x=i;
node.y=j;
node.s=;
qj.push(node);
}
if(a[i][j]=='F'){
b[i][j]=;
node.x=i;
node.y=j;
node.s=;
qf.push(node);
}
}
}
int f=;
while(qj.size()){
int ss=qf.front().s;
while(qf.size()&&ss==qf.front().s){
for(i=;i<;i++){
int tx=qf.front().x+t[i][];
int ty=qf.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#') continue;
if(b[tx][ty]==){
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.s=qf.front().s+;
qf.push(node);
}
}
qf.pop();
}
int sss=qj.front().s;
while(qj.size()&&sss==qj.front().s){
for(i=;i<;i++){
int tx=qj.front().x+t[i][];
int ty=qj.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m){
f=qj.front().s+;
break;
}
if(a[tx][ty]=='#') continue;
if(b[tx][ty]==){
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.s=qj.front().s+;
qj.push(node);
}
}
if(f!=) break;
qj.pop();
}
if(f!=) break;
}
if(f==) printf("IMPOSSIBLE\n");
else printf("%d\n",f);
}
return ;
}
UVA - 11624 Fire! 双向BFS追击问题的更多相关文章
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- UVA - 11624 Fire! 【BFS】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
随机推荐
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
用域名获取IP地址或者用IP获取域名 #include<stdio.h> #include<sys/socket.h> #include<netdb.h> int ...
- 对JavaBean创建的一点改进
在看了<Effective Java>Item2中对JavaBean的描述后,再结合Item1和Builder模式,遂想有没有其他方式避免JavaBean创建的线程安全问题呢? 以如下Ja ...
- [学些东西]用爬虫练习网站来练习burp suite
最近看爬虫的内容.刚好看到黑板客爬虫第二关http://www.heibanke.com/lesson/crawler_ex01. ADO的i春秋课程里面提到的.另外推荐学习爬虫的好书<web ...
- kbmmw 5 的日志备份功能简介
kbmmw 自从4.8.2 版本里增加了日志管理以后,随着版本升级,增加了很多功能,使用方法也有所改变. 功能也越来越强大. 今天说一下 kbmmw5 里面的日志备份,顺便演示一下新的使用方法. 我们 ...
- Sping中的配置Bean详解
一.spring实例化对象的方法 在Spring中,所有管理的对象都是JavaBean对象,而BeanFactory和ApplicationContext就是spring框架的两个IOC容器,现在一般 ...
- MVC设计模式应用
MVC登录程序清单 1 User JAVABean 用户登录操作类,跟数据库中表的信息对应 2 DatabaseConnection JavaBean 负责数据库的连接和关闭操作 3 IUserDAO ...
- (转)基于RTP的H264视频数据打包解包类
最近考虑使用RTP替换原有的高清视频传输协议,遂上网查找有关H264视频RTP打包.解包的文档和代码.功夫不负有心人,找到不少有价值的文档和代码.参考这些资料,写了H264 RTP打包类.解包类,实现 ...
- Android在有存储卡和无存储卡情况下拍照后固定尺寸和压缩大小
我最近工作挺忙,距离上一次写博客转眼已经过了一个多月,每次学到和用到点新东西,其实都有分享的欲望,但奈何文笔太差,而一篇文章包括构思,排版,修改发布的时间最少要花费2个小时(这其中还不包括写完后未保存 ...
- 对于pod导入第三方库文件终端语言记录
//换成 pod install --verbose --no-repo-update //生成Podfile文件 touch Podfile 加上--verbose --no-repo-update ...
- Hadoop合并小文件的几种方法
1.Hadoop HAR 将众多小文件打包成一个大文件进行存储,并且打包后原来的文件仍然可以通过Map-Reduce进行操作,打包后的文件由索引和存储两大部分组成: 缺点: 一旦创建就不能修改,也不支 ...