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
IMPOSSIBLE

【题意】:一个人要逃离迷宫,迷宫中有多处起火了。问能否逃出迷宫,能输出最小步数,不能输出"IMPOSSIBLE"。迷宫的边缘都是出口。

【分析】:双点BFS,火和人同时进行BFS即可。注意首先火源不只一处,可以有多处,那么我们就要把每处火都数组记录下来,然后bfs搜索前让火源全部入队,还有就是不需要逃出地图,只要跑到边界就ok。

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n,m,t;
char a[1005][1005];
int v[1005][1005];
int step[1005][1005];
struct node
{
int x,y,s;
node(){}
node(int xx,int yy,int ss)
{
x=xx;
y=yy;
s=ss;
}
};
bool ok(int x,int y)
{
return x>=0 && y>=0 && x<n && y<m && !v[x][y] && a[x][y]!='#';
}
queue<node>q;
void bfs1() //火
{ while(!q.empty())
{
node st = q.front();
q.pop();
node ed;
for(int i=0; i<4; i++)
{
ed.x = st.x + dir[i][0];
ed.y = st.y + dir[i][1];
if(!ok(ed.x,ed.y)) continue;
step[ed.x][ed.y] = st.s + 1;
v[ed.x][ed.y]=1;
q.push(node(ed.x,ed.y,st.s+1));
}
}
} int bfs2(int x,int y) //人
{
ms(v,0);
v[x][y]=1;
q.push(node(x,y,0));
while(!q.empty())
{
node st = q.front();
q.pop();
node ed;
if(st.x==0||st.x==n-1||st.y==0||st.y==m-1)
{
return st.s+1;
}
for(int i=0;i<4;i++)
{
ed.x = st.x + dir[i][0];
ed.y = st.y + dir[i][1];
if(!ok(ed.x,ed.y)) continue;
if(st.s + 1 < step[ed.x][ed.y]) //人逃跑花的时间比火烧的时间短才行
{
v[ed.x][ed.y]=1;
q.push(node(ed.x,ed.y,st.s+1));
}
}
}
return -1;
} int main()
{
scanf("%d",&t);
while(t--)
{
while(!q.empty()) q.pop();
ms(v,0);
ms(step,INF);
int x,y;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",a[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j] == 'F') //多个用数组存
{
q.push(node(i,j,0));
v[i][j]=1;
step[i][j]=0;
}
if(a[i][j] == 'J')
{
x = i;
y = j;
}
}
}
bfs1();
int ans=bfs2(x,y);
if(ans==-1) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
}

UVA 11624 Fire!【两点BFS】的更多相关文章

  1. UVA 11624 - Fire! 图BFS

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

  2. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  3. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  4. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  5. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  6. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

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

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

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

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

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

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

随机推荐

  1. dnsmasq-2.48没有ipset特性,安装dnsmasq-2.71来支持ipset

    iptables只能根据ip地址进行转发,不能识别域名,而dnsmasq-full不仅可以实现域名-IP的映射,还可以把这个映射关系存储在ipset中,所以使用dnsmasq+ipset就可以实现ip ...

  2. BZOJ1513 [POI2006]Tet-Tetris 3D 【二维线段树】

    题目链接 BZOJ1513 题解 真正地理解了一波线段树标记永久化的姿势 每个节点维护两个值\(v\)和\(tag\) \(v\)代表儿子中的最值 \(tag\)代表未下传的最值 显然节点的区间大于等 ...

  3. java md5加密 不依赖base64包

    /** * MD5 加密 */ private String getMD5Str(String str) { MessageDigest messageDigest = null; try { mes ...

  4. es6+最佳入门实践(7)

    7.set和map数据结构 7.1.什么是set? Set就是集合,集合是由一组无序且唯一的项组成,在es6中新增了set这种数据结构,有点类似于数组,但是它的元素是唯一的,没有重复 let st = ...

  5. 搭建eova开发环境

    1.安装好maven 2.下载Eova项目解压到文件夹eova下 3.dos命令到eova文件夹下执行mvn eclipse:eclipse(注:构建eclipse项目命令) 4.修改*.tag文件错 ...

  6. jvm面试必会基本知识

    内存: 堆区 1.new的对象实例  ps:(java堆可以细分为新生代和老年代)(通过-xmx和-xms来实现可扩展) 虚拟机栈 局部变量 本地方法栈 为虚拟机使用的native方法服务 方法区 s ...

  7. 【BZOJ1146】【CTSC2008】网络管理 [整体二分]

    网络管理 Time Limit: 50 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description M公司是一个非常庞大的跨国公司,在 ...

  8. SSM初步整合一

    SSM(spring+springmvc+mybatis)初步整合一配置步骤: 步骤一.导入所需jar包 步骤二 .配置web.xml文件 <?xml version="1.0&quo ...

  9. 我在一个前端项目中用js整理的一些通用方法,其中使用到的思想,主要就是约定了。

    把名称和后台来的json数据约定起来,可以达到的效果就是可以将东西统一化,减少差异,提升模块等的通用性,此后就可以实现具体不同模块内容可以自动或拷贝赋值的方式 2016.7.18 refactor s ...

  10. 【洛谷 SP2878】Knights of the Round Table(双联通分量)

    先放这吧,没时间写,明天再补 "明天到了" 题目链接 题意:求不在任何奇环内的点的数量. Tarjan求点双联通分量,然后再染色判断是不是二分图就好了. 只是不懂为什么Tarjan ...