题目链接: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即可。

代码实现如下:

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int inf = 0x3f3f3f3f;
 7 int T, r, c, ans, sx, sy;
 8 char mp[1007][1007];
 9 int vis[1007][1007], t[1007][1007];
10
11 struct node{
12     int x, y;
13     int step;
14 }nw, nxt;
15
16 int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
17
18
19 void bfs1() {
20     queue<node> q;
21     for(int i = 0 ; i< r; i++) {
22         for(int j = 0; j < c; j++) {
23             if(mp[i][j] == 'F') {
24                 nw.x = i, nw.y = j;
25                 t[i][j] = 0;
26                 q.push(nw);
27             }
28         }
29     }
30     while(!q.empty()) {
31         nw = q.front(), q.pop();
32         for(int i = 0; i < 4; i++) {
33             nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
34             if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y < c && mp[nxt.x][nxt.y] != '#' && t[nxt.x][nxt.y] > t[nw.x][nw.y] + 1) {
35                 t[nxt.x][nxt.y] = t[nw.x][nw.y] + 1;
36                 q.push(nxt);
37             }
38         }
39     }
40 }
41
42 void bfs2(int x, int y) {
43     nw.x = x, nw.y = y, nw.step = 0;
44     vis[x][y] = 1;
45     queue<node> q;
46     q.push(nw);
47     while(!q.empty()) {
48         nw = q.front(), q.pop();
49         if(nw.x ==0 || nw.x == r - 1 || nw.y == 0 || nw.y == c - 1) {
50             ans = nw.step + 1;
51             return;
52         }
53         for(int i = 0; i < 4; i++) {
54             nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
55             if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y <c && mp[nxt.x][nxt.y] != '#' && nw.step + 1 < t[nxt.x][nxt.y] && vis[nxt.x][nxt.y] == 0) {
56                 vis[nxt.x][nxt.y] = 1;
57                 nxt.step = nw.step + 1;
58                 q.push(nxt);
59             }
60         }
61     }
62 }
63
64 int main() {
65     scanf("%d", &T);
66     while(T--) {
67         scanf("%d%d", &r, &c);
68         for(int i = 0; i < r; i++) {
69             scanf("%s", mp[i]);
70             for(int j = 0; j < c; j++) {
71                 if(mp[i][j] == 'J') {
72                     sx = i, sy = j;
73                 }
74             }
75         }
76         memset(vis, 0, sizeof(vis));
77         memset(t, inf, sizeof(t));
78         ans = inf;
79         bfs1();
80         bfs2(sx, sy);
81         if(ans >= inf) printf("IMPOSSIBLE\n");
82         else printf("%d\n", ans);
83     }
84     return 0;
85 }

转载于:https://www.cnblogs.com/Dillonh/p/8975876.html

Fire! (双bfs+预处理)相关推荐

  1. 【HDU - 1254 】推箱子 (双bfs)

    题干: 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个 ...

  2. HDU - 5637 Transform (思维、bfs预处理)

    HDU - 5637 题目大意: 给出n个数的序列a,对于一个整数x,有两种操作: 1.改变x二进制中任一位 2.将x变为x^a[i] m次查询,每次查询输入两个整数x和y,问x最少经过多少次操作可以 ...

  3. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  4. UVA11624 Fire!(bfs)

    相信大家已经读过题目了,我就搬一下洛谷的翻译: 题目大意 你的任务是帮助Joe走出一个大火蔓延的迷宫.Joe每分钟可以走到上下左右4个方向的相邻格子之一,而所有着火的格子都会四周蔓延(即如果某个空格子 ...

  5. 杭电1254java实现(双bfs 优先队列)

    推箱子 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个 ...

  6. FZU 2150 Fire Game bfs

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103921#problem/I bfs水题.好像还做过一次了.思路题意都见代码吧 ...

  7. 【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)

    题干: The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the gala ...

  8. hdu-2612-Find a way(双bfs)

    圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤神:我要去左边的这个(因为离自己比较近 哈哈~)..瑞瑞:我要 ...

  9. luogu P3393 逃离僵尸岛(点权最短路 + 多源BFS)

    P3393 逃离僵尸岛 最短路,有点的不能到达我们就直接把他的权值赋值为INF即可. bfs预处理一下每个点的危险程度. 因为这里没有边权是点权,我们可以把边权转化为两端点的权值和,或者直接跑点权最短 ...

  10. J - Fire! UVA - 11624

    J - Fire! UVA - 11624 题意:火每次能烧到上下左右,人碰到非墙的边界则逃火成功,求最短的逃离时间. 由于bfs每个位置最多入队出队一次,所以复杂度为 1e6 一发bfs直接TLE, ...

最新文章

  1. ArcGIS网络分析之构建网络分析数据集(一)
  2. Linux笔记 1-8 --文件传输
  3. OPW-00001: Unable to open password-file
  4. 使用JPA标准@ViewScoped通过分页,过滤和排序进行Primefaces DataTable延迟加载
  5. Linux做施压机的最大线程数,关于性能测试的几个要点
  6. 最简单的Jdbc连接Oracle代码
  7. 140:Bandwidth
  8. 【Android】16.5 Android内置的系统服务
  9. 抓虫系列(三) 不要轻视web程序中常用的三个池 之数据库连接池
  10. 03-创建模型操作---用户添加
  11. excel取整数的函数_Excel教程:取整函数INT 与TRUNC~~Excel新技能
  12. 【直接下载】x86_64-6.3.0-release-win32-seh-rt_v5-rev2
  13. 3dmax2020软件安装教程
  14. 032-OpenCV模板匹配单个对象、多个对象
  15. 指针(一)(基本概念)
  16. matlab 运动检测,如何使用MATLAB进行运动目标的检测
  17. b站直播html5黑屏,用bilibili直播姬的抓屏为什么是黑屏而不是界面呢
  18. 计算机桌面截图怎么截,电脑如何截图,教您电脑截图怎么截
  19. 嵌入式C设计模式---状态机设计模式
  20. Hibernate HQL 语法大全(下)

热门文章

  1. AndroidStudio配置NDK
  2. 抗击疫情,宜复辟拱手礼
  3. 要尊重员工的正当权益
  4. TestRange.cs error CS0104: `Range' is an ambiguous reference between `System.Range' and Gtk.Range
  5. 自己都看到满眼问题,还要请专业测试?
  6. Factory Method (工厂方法)
  7. linux 源码包解压编译安装
  8. 得到照片_用PS制作重曝效果的人像艺术照片
  9. 电脑机器人_磨小分校参加成都市“青少年电脑机器人创新实践活动”巡航者决赛...
  10. 经典vim插件功能说明、安装方法和使用方法