Problem B: Fire!

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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE
讲解:写了一下午还是没写出来,这种题还没有做过,的确很难想到用这种方法;
参考的别人的代码,两个搜索,比较人到这个地方时,火来了没有;
 #include<stdio.h>
#include<string.h>
int T,m,n,ans,ok,step[][],bu[][]; // step用来初始化火,bu用来记录人
char mat[][];
struct C
{
int x,y;
}q[];
int dx[]={,,-,};
int dy[]={,,,-};
void fire()
{
int fr=,re=;
memset(step,-,sizeof(step)); //相当于标记访问,同时又记录步数
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(mat[i][j]=='F')
{
q[re].x=i;
q[re++].y=j;
step[i][j]=;
}
}
while(fr<re) {
int x=q[fr].x;
int y=q[fr].y;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=n || nx< || ny>=m || ny<) continue;
if(step[nx][ny]!=-) continue;
if(mat[nx][ny]=='#') continue;
step[nx][ny]=step[x][y]+;
q[re].x=nx;
q[re++].y=ny;
}
fr++;
}
}
void bfs()
{
memset(bu,-,sizeof(bu)); //同理
int fr=,re=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(mat[i][j]=='J') {
q[re].x=i;
q[re++].y=j;
bu[i][j]=;
}
while(fr<re)
{
int nx=q[fr].x;
int ny=q[fr].y;
for(int i=;i<;i++)
{
int fx=nx+dx[i];
int fy=ny+dy[i];
if(nx== || nx==n- || ny== || ny==m-) {
ok=;
ans=bu[nx][ny]+;
return ;
}
if(fx>=n || fx< || fy>=m || fy<) continue;
if(bu[fx][fy]!=-) continue;
if(mat[fx][fy]=='#') continue; /* 须特别注意,火有可能被墙包围,使得火无法蔓延。下句的意思是,如果火蔓延到这个格子,
并且火到这个格子的步数小于或等于人到这个格子的步数,那么跳过此格子。假使火根本没有蔓延过来
,理所当然可以人走这个格子。 */
if(step[fx][fy]!=- && bu[nx][ny]+>=step[fx][fy]) continue;
q[re].x=fx;
q[re++].y=fy;
bu[fx][fy]=bu[nx][ny]+; }
fr++;
}
}
int main()
{
scanf("%d%*c",&T);
while(T--) {
scanf("%d %d%*c",&n,&m);
for(int i=;i<n;i++)
gets(mat[i]);
fire();
ok=;
bfs();
if(ok) printf("%d\n",ans);
else printf("IMPOSSIBLE\n");
}
return ;
}

UVA Problem B: Fire!的更多相关文章

  1. FZU Problem 2150 Fire Game

    Problem 2150 Fire Game Accept: 145    Submit: 542 Time Limit: 1000 mSec    Memory Limit : 32768 KB P ...

  2. FZOJ Problem 2150 Fire Game

                                                                                                        ...

  3. 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  4. (UVA 11624)Fire!

    题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...

  5. 【UVA - 11624】Fire!

    -->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...

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

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

  7. UVa 11624 (BFS) Fire!

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

  8. UVa Problem 10132 File Fragmentation (文件还原) 排列组合+暴力

    题目说每个相同文件(01串)都被撕裂成两部分,要求拼凑成原来的样子,如果有多种可能输出一种. 我标题写着排列组合,其实不是什么高深的数学题,只要把最长的那几个和最短的那几个凑一起,然后去用其他几个验证 ...

  9. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

随机推荐

  1. [Ubuntu Setup] Ubuntu 13.04 安装 ia32-libs

    http://stackoverflow.com/questions/23182765/how-to-install-ia32-libs-in-ubuntu-14-04-lts sudo -i cd ...

  2. ylbtech-LanguageSamples-UserConversions(用户定义的转换)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-UserConversions(用户定义的转换) 1.A,示例(Sample) 返回顶部 ...

  3. enter 键登录的实现

    js 代码 document.onkeypress = function() { var iKeyCode = -1; if (arguments[0]) { iKeyCode = arguments ...

  4. Android面试题(2)

    1.activity的生命周期 方法 描述 可被杀死 下一个 onCreate() 在activity第一次被创建的时候调用.这里是你做所有初始化设置的地方──创建视图.设置布局.绑定数据至列表等.如 ...

  5. JMS之——ActiveMQ高可用+负载均衡集群

    一.高可用集群 从ActiveMQ5.9开始,ActiveMQ的集群实现方式取消了传统的Master-Slave方式,增加了基于ZooKeeper+LevelDB的Master-Slave实现方式,其 ...

  6. selenium 问题:加了显性等待后,操作元素依然出错

    背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待元素出现.结果屡屡出问题.元素默认是隐藏的,导致等待过早的就结 ...

  7. Android File类 根据官方文档理解

    File有四个构造函数        public File(File dir,String name)             参数为File和String,File制定构造的新的File对象的路径 ...

  8. Unity3d 4.3 通过代码动态更改SpriteRender的Sprite

    http://www.unitymanual.com/home.php?mod=space&uid=2452&do=blog&id=420 using UnityEngine; ...

  9. 算法笔记_103:蓝桥杯练习 算法提高 金明的预算方案(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些 ...

  10. 彻底删除Oracle 11g的方法

    1.关闭oracle所有的服务. 可以在windows的服务管理器中关闭. 2.打开注册表:regedit 打开路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlS ...