11624 - Fire!

Time limit: 1.000 seconds

Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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

开始提议理解错了,wrong了好几次,才知道原来可以有多个火源地。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 1005
char s[MAXN];
int ma[MAXN][MAXN];
bool vis[MAXN][MAXN];
int d[][] = {{, }, {, }, {, -}, {-, }};
bool flag;
struct P
{
int x, y, s;
P() : x(), y(), s() {}
P(int x_, int y_, int s_) : x(x_), y(y_), s(s_) {}
};
P J, F;
queue<P> q, Q;
int r, c; int Judge(int x, int y)
{
if(x > && y > && x <= r && y <= c) return ;
return ;
} void init()
{
while(!q.empty()) q.pop();
while(!Q.empty()) Q.pop();
_cle(ma, );
_cle(vis, false);
flag = false;
} int bfs()
{
vis[J.x][J.y] = ;
q.push(J);
int last = ;
while(!q.empty())
{
P t = q.front();
q.pop();
if(t.x == || t.y == || t.x == r || t.y == c)
return t.s + ;
if(flag && last != t.s + )
{
int siz = Q.size();
while(siz--)
{
P p = Q.front();
Q.pop();
repu(i, , )
{
int dx = p.x + d[i][];
int dy = p.y + d[i][];
if(Judge(dx, dy) && ma[dx][dy] == )
{
ma[dx][dy] = ;
Q.push(P(dx, dy, t.s));
}
}
}
last = t.s + ;
}
repu(i, , )
{
int dx = t.x + d[i][];
int dy = t.y + d[i][];
if(Judge(dx, dy) && !vis[dx][dy] && ma[dx][dy] == )
{
vis[dx][dy] = ;
q.push(P(dx, dy, t.s + ));
}
}
}
return -;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &r, &c);
repu(i, , r)
{
scanf("%s", s);
repu(j, , c) if(s[j] == '.') ma[i + ][j + ] = ;
else if(s[j] == 'F')
{
Q.push(P(i + , j + , ));
flag = true;
ma[i + ][j + ] = ;
}
else if(s[j] == 'J')
{
J.x = i + , J.y = j + , J.s = ;
ma[i + ][j + ] = ;
}
}
int re = bfs();
if(re == -) printf("IMPOSSIBLE");
else printf("%d", re);
puts("");
}
return ;
}

uva 11624(bfs)的更多相关文章

  1. UVA 11624 BFS的妙用

    题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...

  2. UVa 11624 (BFS) Fire!

    也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...

  3. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  4. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  5. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  6. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  7. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  8. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  9. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. Sqlserver2008日志压缩

    SqlServer2008日志压缩语句如下: USE [master] GO ALTER DATABASE DBName SET RECOVERY SIMPLE WITH NO_WAIT GO ALT ...

  2. [SAP ABAP开发技术总结]EXIT-COMMAND

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. 学习Berkeley DB- 入门

    1 导言 首先,我们要了解Berkeley DB的一些基本特性,在IBM的开发网站上有篇文章对其有比较清晰的介绍: 这篇文章讲到了BDB的设计思想和核心数据结构.以及数据访问算法:并有常用函数使用范例 ...

  4. python_way day16 JQuary

    python_way day16 JQuery 封装dom js代码 jQuery(1.10,1.12-兼容性好,2.0.以后放弃了ie9以下) - 封装了Dom & JavaScript 查 ...

  5. 关于Android构建

    “IDE都是给小白程序员的,大牛级别的程序员一定是命令行控,终端控,你看大牛都是使用vim,emacs 就一切搞定” 这话说的虽然有些绝对,但是也不无道理,做开发这行要想效率高,自动化还真是缺少不了命 ...

  6. weblogic与axis2 jar包冲突

    1.org.springframework.web.util.NestedServletException: Handler processing failed; nested exception i ...

  7. Python学习(10)元组

    目录 Python 元组 访问元组 修改元组 删除元组 元组运算符 元组索引,截取 无关闭分隔符 元组内置函数 Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组 ...

  8. 统一事件源epoll代码示例

    可以将信号注册进pipe管道的写端,通过对读端的监听,来实现统一事件源. #include <sys/types.h> #include <sys/socket.h> #inc ...

  9. TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  10. SDL2.0的加载图片贴图

    加载图片贴图,采用了SDL_Window.SDL_Renderer.SDL_Texture和SDL_Image库 实例: #include <stdio.h> #include <m ...