Fire! (双bfs+预处理)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671
题目:
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 IMPOSSIBL
题意:Joe要逃离着火的森林,Joe和火都只能往上下左右四个方向转移,Joe到达边界即可离开,问最小逃离步数,如果不能输出IMPOSSIBL。坑点:是逃离后的步数,因而要在到达边界的步数上+1,火有多个(因为这个WA了好久==!)。
思路:先用t数组预处理出火到达每个地方的时间,然后再对Joe进行bfs即可。
代码实现如下:
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; const int inf = 0x3f3f3f3f;
int T, r, c, ans, sx, sy;
char mp[][];
int vis[][], t[][]; struct node{
int x, y;
int step;
}nw, nxt; int dx[] = {, -, , }, dy[] = {, , , -}; void bfs1() {
queue<node> q;
for(int i = ; i< r; i++) {
for(int j = ; j < c; j++) {
if(mp[i][j] == 'F') {
nw.x = i, nw.y = j;
t[i][j] = ;
q.push(nw);
}
}
}
while(!q.empty()) {
nw = q.front(), q.pop();
for(int i = ; i < ; i++) {
nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
if(nxt.x >= && nxt.x < r && nxt.y >= && nxt.y < c && mp[nxt.x][nxt.y] != '#' && t[nxt.x][nxt.y] > t[nw.x][nw.y] + ) {
t[nxt.x][nxt.y] = t[nw.x][nw.y] + ;
q.push(nxt);
}
}
}
} void bfs2(int x, int y) {
nw.x = x, nw.y = y, nw.step = ;
vis[x][y] = ;
queue<node> q;
q.push(nw);
while(!q.empty()) {
nw = q.front(), q.pop();
if(nw.x == || nw.x == r - || nw.y == || nw.y == c - ) {
ans = nw.step + ;
return;
}
for(int i = ; i < ; i++) {
nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
if(nxt.x >= && nxt.x < r && nxt.y >= && nxt.y <c && mp[nxt.x][nxt.y] != '#' && nw.step + < t[nxt.x][nxt.y] && vis[nxt.x][nxt.y] == ) {
vis[nxt.x][nxt.y] = ;
nxt.step = nw.step + ;
q.push(nxt);
}
}
}
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &r, &c);
for(int i = ; i < r; i++) {
scanf("%s", mp[i]);
for(int j = ; j < c; j++) {
if(mp[i][j] == 'J') {
sx = i, sy = j;
}
}
}
memset(vis, , sizeof(vis));
memset(t, inf, sizeof(t));
ans = inf;
bfs1();
bfs2(sx, sy);
if(ans >= inf) printf("IMPOSSIBLE\n");
else printf("%d\n", ans);
}
return ;
}
Fire! (双bfs+预处理)的更多相关文章
- uva11624 Fire! (bfs预处理)
题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...
- BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!
绝世污题,垃圾题,浪费我一整天青春! 1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1262 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)
Description In country Light Tower, a presidential election is going on. There are two candidates, ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- bzoj 1415(概率dp和bfs预处理)
感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec M ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
随机推荐
- iOS开发给UIView添加动画Animation
self.testView需要添加动画的view 1.翻转动画 [UIView beginAnimations:@"doflip" context:nil]; [UIView se ...
- iOS开发应用程序更新
#import "ViewController.h" //1一定要先配置自己项目在商店的APPID,配置完最好在真机上运行才能看到完全效果哦 #define STOREAPPID ...
- TCP系列06—连接管理—5、TCP fastopen(TFO)
一.TFO背景 当前web和web-like应用中一般都是在三次握手后开始数据传输,相比于UDP,多了一个RTT的时延,即使当前很多应用使用长连接来处理这种情况,但是仍然由一定比例的短连接,这额外多出 ...
- 重新看《JavaScript高级程序设计》
几点心得: 1)数据是基础,一共有3种基础数据:null.undefined.和object:遵循从无到有从简单到复杂的演变过程 2)衍生数据:衍生数据是指操作符合语句,这些是基础数据产生导致的必然结 ...
- Sqoop 1.4.6 安装配置
配置环境变量 # SQOOP SQOOP_HOME=/home/hadoop/development/src/sqoop-1.4.6-cdh5.6.0 PATH=$PATH:$SQOOP_HOME/b ...
- jenkins部署springboot多项目
war包的部署问题不大,这里记录jar包的部署过程: 1:jar包的体积过大问题 pom.xml参考以下配置(依赖包会分离到target/lib/,jar包体积由几十M缩小到几k) <build ...
- MySQL常用存储引擎功能与用法详解
本文实例讲述了MySQL常用存储引擎功能与用法. MySQL存储引擎主要有两大类: 1. 事务安全表:InnoDB.BDB. 2. 非事务安全表:MyISAM.MEMORY.MERGE.EXAMPLE ...
- (二)MySQL学习笔记
1.视图 视图是一系列select语句返回的可视化结果集,是一张虚拟表.更多介绍请查看http://tool.oschina.net/apidocs/apidoc?api=mysql-5.1-zh 视 ...
- 快速搭建http服务:共享文件--Java的我,不知Python你的好
在 Linux 服务器上或安装了 Python 的机器上, 我们可以在指定的文件目录下,使用 python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web ...
- 实例——简单的Samba共享
服务端配置 # 临时停止iptables service iptables stop # 临时禁用SELinux setenforce 0 # 禁止iptables开机自动启动 chkconfig i ...