UVA Problem B: Fire!
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!的更多相关文章
- FZU Problem 2150 Fire Game
Problem 2150 Fire Game Accept: 145 Submit: 542 Time Limit: 1000 mSec Memory Limit : 32768 KB P ...
- FZOJ Problem 2150 Fire Game
...
- 【转】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 / ...
- (UVA 11624)Fire!
题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...
- 【UVA - 11624】Fire!
-->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...
- UVA - 11624 J - Fire! (BFS)
题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...
- UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
- UVa Problem 10132 File Fragmentation (文件还原) 排列组合+暴力
题目说每个相同文件(01串)都被撕裂成两部分,要求拼凑成原来的样子,如果有多种可能输出一种. 我标题写着排列组合,其实不是什么高深的数学题,只要把最长的那几个和最短的那几个凑一起,然后去用其他几个验证 ...
- FZU Problem 2150 Fire Game(bfs)
这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
随机推荐
- 1:MUI选择器组件抛出“n.getSelectedItem is not a function”异常的解决办法 2:mui三级联动 3:移动端关闭虚拟键盘
1:如下图 问题:引用了mui的地址选择的三级联动的应用在h5上的组件 百度发现别人思路对 Array 原型链方法扩充时,会抛出这个异常. 修改方法: mui.poppicker.js 第 112 行 ...
- [转]SQL Server 创建数据库邮件
本文转自:http://www.cnblogs.com/gaizai/p/3358958.html 一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. ...
- transparent shadow caster unity
https://forum.unity.com/threads/semitransparent-shadows.276490/ semitransparent shadows dither 类似alp ...
- Intellij IDEA中使用log4j日志
一.在pom.xml中添加依赖 <dependency> <groupId>log4j</groupId> <artifactId>log4j</ ...
- 从word中导出图片
想把word里面的图片导出来,可以这么操作: 1.右键word里面的图片,复制 2.打开电脑的画图工具,粘贴,然后保存
- ios 程序发布使用xcode工具Application Loader 正在通过ITUNES STORE进行鉴定错误
ios 程序发布使用xcode工具Application Loader 正在通过ITUNES STORE进行鉴定错误 一:此错误会导致上传程序,一直停留在验证阶段,而没有一点上传进度:结果会苦等半天, ...
- Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment
你能够把fragment看成是activity的模块化部分.它拥有自己的生命周期,接受它自己的输入事件,你能够在activity执行时加入或者删除它(有点像是一个"子activity&quo ...
- Protocol Informatics (PI项目)【基于网络轨迹的协议逆向工程文献学习】
Protocol Informatics[基于网络轨迹的协议逆向工程文献学习]by tsy 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途.恕作者著作 ...
- LaTeX 图片插入
一般格式: \begin{figure} \centering \includegraphics[scale=0.2]{Figure211.png} \caption{In the case of a ...
- windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决 一.发现问题 由于tomcat内存溢出,在wind ...