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 ...
随机推荐
- mysql向上递归&向下递归
工作记录 向上递归函数test: BEGIN ); ); SET sTemp = '$'; SET sTempChd =cast(rid as CHAR); WHILE sTempChd is not ...
- svn checkout不包括根目录
在后面加 “.” 即可,如下: svn co svn://127.0.0.1/ylshop/ . 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/
- jQuery的动画与特效
显示与隐藏 show() 和 hide() 方法 动画效果的show() 和 hide() show(speed,[]callback) hide(speed,[]callback) speed:表示 ...
- 【bzoj1692】[Usaco2007 Dec]队列变换 贪心+后缀数组
题目描述 FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- Python 源码剖析(二)【整数对象】
二.整数对象 1.PyIntObject 2.PyIntObject 对象的创建和维护 3.Hack PyIntObject 1.PyIntObject PyIntObject的定义: [intobj ...
- NetScaler VLAN’s Demystified
NetScaler VLAN’s Demystified https://www.citrix.com/blogs/2014/12/29/netscaler-vlans-demystified/ Th ...
- 【刷题】洛谷 P3804 【模板】后缀自动机
题目描述 给定一个只包含小写字母的字符串 \(S\) , 请你求出 \(S\) 的所有出现次数不为 \(1\) 的子串的出现次数乘上该子串长度的最大值. 输入输出格式 输入格式: 一行一个仅包含小写字 ...
- [洛谷P5169]xtq的异或和
题目大意:给你一张$n(n\leqslant10^5)$个点$m(m\leqslant3\times10^5)$条边的无向图,每条边有一个权值,$q(q\leqslant2^{18})$次询问,每次询 ...
- 消息传递 树形DP
非常妙的树形DP:由于n很小,我们可以枚举每一个点作为第一个节点,计算其时间花费 那么问题就转化为对于给点节点求花费时间. 通过观察,显然我们会发现先传给花费时间多的人更加合算,因为这样可以最大限度的 ...