题目链接: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+预处理)的更多相关文章

  1. uva11624 Fire! (bfs预处理)

    题目链接:https://vjudge.net/problem/UVA-11624 题意:给一个1000×1000的矩阵,有几个着火点和Joe,着火点和Joe每个单位时间均移动一个单位,求Joe逃出的 ...

  2. BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!

    绝世污题,垃圾题,浪费我一整天青春! 1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1262 ...

  3. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  4. 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

    Description In country Light Tower, a presidential election is going on. There are two candidates,   ...

  5. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  6. bzoj 1415(概率dp和bfs预处理)

    感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  M ...

  7. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  8. FZU - 2150 Fire Game bfs+双起点枚举

    题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...

  9. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

随机推荐

  1. 《学习OpenCV》课后习题解答6

    题目:(P104) 使用cvCmp()创建一个掩码.加载一个真实的图像.使用cvsplit()将图像分割成红,绿,蓝三个单通道图像. a.找到并显示绿图. b.克隆这个绿图两次(分别命名为clone1 ...

  2. netbeans调试配置

    apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...

  3. Swift & Unicode

    Swift & Unicode emoji let == const https://www.runoob.com/swift/swift-basic-syntax.html

  4. 几种常见web 容器比较

     1:产品介绍 WebLogic是美国bea公司出品的一个application server确切的说是一个基于j2ee架构的中间件.BEA WebLogic是用于开发.集成.部署和管理大型分布式We ...

  5. Django错误 OperationalError: no such column: xxx

    模型前后操作如下: 第一次迁移: class Snippet(models.Model): created = models.DateTimeField(auto_now_add=True) titl ...

  6. [LOJ2538] [PKUWC2018] Slay the Spire

    题目链接 LOJ:https://loj.ac/problem/2538 Solution 计数好题. 首先可以发现这题和期望没关系. 其次对于手上的一套牌,设我们有\(a\)张强化牌,那么: 如果\ ...

  7. 【BZOJ1031】字符加密(后缀数组)

    [BZOJ1031]字符加密(后缀数组) 题面 BZOJ 洛谷 题解 把字符串倍长 然后直接求后缀数组, 拍好序之后直接输出就行了. (我只是复习一下\(SA\)而已) #include<ios ...

  8. POJ3057:Evacuation——题解

    http://poj.org/problem?id=3057 题目大意: .为人,D为门,X为障碍,门每秒只能出去一个人,问多少秒出光. 如果无法出光输出impossible. ——————————— ...

  9. 洛谷 P4592 [TJOI2018]异或 解题报告

    P4592 [TJOI2018]异或 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1 x y:查 ...

  10. 差点AFO

    差点就AFO了,小伙伴们一定注意护眼啊. 眼睛总算是活过来了. 还有一个月联赛,加油