题目链接 http://vjudge.net/contest/121377#problem/J

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 ≤ R, C ≤ . 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 ####
#JF#
#..#
#..#
###
#J.
#.F
Sample Output IMPOSSIBLE

题意:有个人在一个R*C的迷宫里,迷宫的某些点有火源,以每秒一格的速度向四周扩散,墙过不去,给出人的坐标,走到边缘就算逃出,问这个人是否能成功逃出,如逃出输出需要几秒?如不能输出-1;

方法:先让所有的火向四周扩散,再看这个人是否能逃出去

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 1009
int w[N][N],ww[N][N];
int a[][]={{,},{-,},{,},{,-}};
char str[N][N];
int n,m;
struct node
{
int x,y,s;
};
queue<node> Q;
node e,f,q,p;
void qq()///求火能蔓延的到所能蔓延的点得时间
{
memset(ww,INF,sizeof(ww));
while(Q.size())
{
p=Q.front();Q.pop();
ww[p.x][p.y]=p.s;
for(int i=;i<;i++)
{
q.x=p.x+a[i][];
q.y=p.y+a[i][];
q.s=p.s+;
if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&str[q.x][q.y]!='#'&&!w[q.x][q.y])
{
w[q.x][q.y]=;
Q.push(q);
}
}
}
}
int dfs()///人从起点开始走所有点遍历,看是否是到出口
{
qq();
memset(w,,sizeof(w));
queue<node> o;
o.push(f);
while(o.size())
{
p=o.front();o.pop();
if(p.x==||p.x==n-||p.y==||p.y==m-)
return p.s+;
for(int i=;i<;i++)
{
q.x=p.x+a[i][];
q.y=p.y+a[i][];
q.s=p.s+;
if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&!w[q.x][q.y]
&&str[q.x][q.y]=='.'&&q.s<ww[q.x][q.y])
{
w[q.x][q.y]=;
o.push(q);
}
}
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
met(w,);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",str[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='F')///不止一个火源,找到一个存一个
{
e.x=i;e.y=j;e.s=;
w[i][j]=;Q.push(e);
}
if(str[i][j]=='J')
{
f.x=i;f.y=j;
f.s=;
}
}
}
int ans;
ans=dfs();
if(ans==-) printf("IMPOSSIBLE\n");
else
printf("%d\n",ans);
}
return ;
}

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

  1. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  2. 并查集(UVA 1106)

    POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...

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

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

  4. L-Gap Substrings(uva 10829)

    题意:有一种形如uvu形式的字符串,其中u是非空字符串,且V的长度正好为L,那么称这个字符串为L-Gap字符串 给出一个字符串S,以及一个正整数L,问S中有多少个L-Gap子串. /* 这道题用到一个 ...

  5. Minimum Sum LCM(uva 10791)

    题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1 ...

  6. Killer Problem (UVA 11898 )

    Problem You are given an array of N integers and Q queries. Each query is a closed interval [l, r]. ...

  7. POJ 2250 Compromise (UVA 531)

    LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...

  8. 【UVA - 11624】Fire!

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

  9. uva 1639--精度处理方法之取对数(uva 1639)

    1639 - Candy Time limit: 3.000 seconds 1639 CandyLazyChild is a lazy child who likes candy very much ...

随机推荐

  1. JAXB完毕XML与Java对象的互转

    这段时间都老忙了,甚至连周末全部人员都在赶产品的进度,想想连续上12天班,人都有点晕了! 到这会儿最终有点时间.所以准备和大家分享一下JAXB,会不会有人认为有点陌生呢?没事,这里跟大伙儿简单的描写叙 ...

  2. typedef 优于 #define

    案例一: 通常讲,typedef要比#define要好,特别是在有指针的场合.请看例子: typedef char *pStr1; #define pStr2 char *; pStr1 s1, s2 ...

  3. 哈夫曼(Huffman)编码

    哈夫曼编码(Huffman Coding)是一种非常经典的编码方式,属于可变字长编码(VLC)的一种,通过构造带权路径长度最小的最优二叉树以达到数据压缩的目的.哈弗曼编码实现起来也非常简单,在实际的笔 ...

  4. 通用PE u盘装Ghost Win7系统

    http://www.tongyongpe.com/win7ghost.html 导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有 ...

  5. textarea 中的换行符问题

    下面是我对这个问题的解决过程,最后算是完全搞懂了,真是阴沟里险些翻船 1.必须知道textarea中的换行符是 \n  (个人检测发现按回车键是\n,好像在linux下是\r\n) 2.用nl2br之 ...

  6. HBase-再看HBase

    为什么HBase主要应用于在线应用系统?(还没搞懂) www.github.com/HBaseinaction google 三大论文  Google File System MapReduce Bi ...

  7. 炼数成金hadoop视频干货01

    视频地址:http://pan.baidu.com/s/1dDEgKwD 最开始还是讲hadoop的起源,但是和其他垃圾视频不同,不是照本宣科,听了还是受益.作者给人一种感觉就是他是确实把他的经验和体 ...

  8. Android自定义View,高仿QQ音乐歌词滚动控件!

    最近在以QQ音乐为样板做一个手机音乐播放器,源码下篇博文放出.今天我想聊的是这个QQ音乐播放器中歌词显示控件的问题,和小伙伴们一起来探讨怎么实现这个歌词滚动的效果.OK,废话不多说,先来看看效果图: ...

  9. hadoop学习记录(三)HBase基本概念

    这一次开始学习HBase数据库. 我用的是VMWare + ubuntu16.04 +Hbase1.1.5 +hadoop2.6.0的组合. 经过亲自安装验证,版本间没有发生冲突,可以进行学习和开发. ...

  10. javaweb学习总结二(静态导入、自动拆装箱、增强for与可变参数)

    一:静态导入语法:import static 导入类.成员变量|成员方法那么在代码中可以直接使用变量或者方法,而不需要通过类名.来调用 静态导入: import static java.lang.Sy ...