Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu

Description

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 the two integers  R and  C, separated by spaces, with 1 <=  RC <= 1000. The following  R lines of input 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 the input.

Output

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

Case #1:
4 4
####
#JF#
#..#
#..# Case #2:
3 3
###
#J.
#.F

Sample Output

3
IMPOSSIBLE

Source

waterloo 13 June, 2009

题目本身不难,但是很容易WA,两个坑点:一开始就在边界的情况和火堆根本碰不到人的情况,第一种稍微把BFS判断顺序改一下就可以了,第二种会导致比较人和火谁先跑出迷宫这种BFS方法WA,可以假设若火根本碰不到人,那么就算火跑的比香港记者更快,那人还是可以走出去的,然后怎么保证每一个地方都是最早被火碰到的呢?把所有的火都压入队列一次性BFS掉,这样保证层数相同时时间可以统一更新,因此要用另一种保险的BFS方法,把每一个点时间设好,再对人进行BFS,被这两个坑点弄的WA十余次………哎还是too
naive。找了个只是输出格式修改了的题面放上去,方便起见自己把输出格式也改成了原题一样的……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0); const int N=1010;
struct info
{
int x;
int y;
int t;
info operator+(info b)
{
b.x+=x;
b.y+=y;
b.t+=t;
return b;
}
};
info J,F,direct[4]={{1,0,1},{-1,0,1},{0,1,1},{0,-1,1}};
char pos[N][N];
int firetime[N][N];
int vis[N][N];
int n,m;
queue<info>Q;
inline bool checkfire(const info &f)
{
return (f.x>=0 && f.x<n && f.y>=0 && f.y<m && pos[f.x][f.y]!='#' && vis[f.x][f.y]==0);
}
inline bool checkman(const info &a)
{
return (a.x>=0 && a.x<n && a.y>=0 && a.y<m && pos[a.x][a.y]!='#' && pos[a.x][a.y]!='F' && vis[a.x][a.y]==0);
}
inline void init()
{
MM(pos,0);
MM(firetime,INF);
J.x=-1,J.y=-1,J.t=-1;
while (!Q.empty())
Q.pop();
}
inline void setfire()
{
MM(vis,0);
while (!Q.empty())
{
info now=Q.front();
Q.pop();
for (int i=0; i<4; ++i)
{
info v=now+direct[i];
if(checkfire(v))
{
vis[v.x][v.y]=1;
firetime[v.x][v.y]=v.t;
Q.push(v);
}
}
}
}
inline int run(const info &s)
{
MM(vis,0);
queue<info>q;
vis[s.x][s.y]=1;
q.push(s);
while (!q.empty())
{
info now=q.front();
q.pop();
if(now.x==0||now.x==n-1||now.y==0||now.y==m-1)
return now.t;
for (int i=0; i<4; ++i)
{
info v=now+direct[i];
if(checkman(v)&&v.t<firetime[v.x][v.y])
{
vis[v.x][v.y]=1;
q.push(v);
}
}
}
return -1;
}
int main(void)
{
int tcase,i,j,out,cnt;
scanf("%d",&tcase);
while (tcase--)
{
init();
cnt=0;
out=-1;
scanf("%d%d",&n,&m);
for (i=0; i<n; ++i)
{
scanf("%s",pos[i]);
for (j=0; j<m; ++j)
{
if(pos[i][j]=='J')
{
J.x=i;
J.y=j;
J.t=0;
}
else if(pos[i][j]=='F')
{
F.x=i;
F.y=j;
F.t=0;
Q.push(F);
}
}
}
setfire();
out=run(J);
out==-1?puts("IMPOSSIBLE"):printf("%d\n",out+1);
}
return 0;
}

UVa 11624 Fire!(BFS)的更多相关文章

  1. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

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

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

  3. UVA 11624 - Fire! 图BFS

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

  4. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. UVA - 11624 J - Fire! (BFS)

    题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...

  6. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

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

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

  8. UVA - 11624 Fire! 【BFS】

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

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

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

随机推荐

  1. Spring配置JNDI的解决方案

    我的配置环境是:Spring + Tomcat + MySql 说明: 1. $TOMCAT_HOME代表Tomcat的安装目录. 第一步:在Tomcat的$TOMCAT_HOME/conf/cont ...

  2. Hudson可扩展持续集成引擎

    参考文章:http://blog.csdn.net/dazhi_100/article/details/11629133 极限编程中一项建议实践便是持续集成,持续集成是指在开发阶段,对项目进行持续性自 ...

  3. WIN7实现多人远程一台电脑

    今天查了查网,发现有人说,WIN7可以实现多人远程一台电脑,于是乎我就试了试, 在工作办公室里的局域网里试了试,嘿,成功了,愿与大家分享一下,呵呵! 方法一: 多用户早就能破解了 方法如下:用UE打开 ...

  4. p168习题

  5. [转]Java多线程编程的常见陷阱

    1.在构造函数中启动线程 我在很多代码中都看到这样的问题,在构造函数中启动一个线程,类似这样: public class A{ public A(){ this.x=1; this.y=2; this ...

  6. **对比$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')

    最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下. public function __construct($token, $wxuser = ''){ $ ...

  7. 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)

    Laravel  和 Laravel  的邮件发送使用方式完全一致.Laravel  的邮件发送中文文档在:http: 邮箱为例,展示如何用 Laravel 内置的邮件发送类来发送邮件. 配置 修改邮 ...

  8. &是什么运算符(转)

    &表示两种运算符,其中一种表示取值运算符,一种是按位与 取值运算符 int a=1; int *p=&a; //其中&a表示的就是把a中的地址取出来,然后赋给指针变量,也就是说 ...

  9. FPGA学习

    (一)Verilog HDL语法 一.模块 1.定义:一个电路模块/一种逻辑功能: 2.命名规则:只能是字母,数字,"$",或者'_',且开头必须是字母或者"_" ...

  10. ScrollView与ListView的冲突

    众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...