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次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
随机推荐
- 以下内容为Stackoverflow上整理以作纪录
PRO 用IMG标签 Use IMG plus alt attribute if the image is part of the content such as a logo or diagram ...
- linux安装ping
https://www.cnblogs.com/iamdevops/p/5743157.html 使用docker仓库下载的ubuntu 14.04 镜像.里面精简的连 ping 命令都没有.goog ...
- sql server阻塞(block)处理
sp_who2 ACTIVE --从下图可知spid = 65进程被76阻塞 --或 * FROM sys.sysprocesses WHERE blocked <> 0 ) --查看阻塞 ...
- 关于TagHelper的那些事情——如何自定义TagHelper(TagHelper基类)
写在开头 前面介绍了TagHelper的基本概念和内嵌的TagHelpers,想必大家对TagHelper都有一定的了解.TagHelper看上去有点像WebControl,但它不同于WebContr ...
- 使用weinre调试手机页面
阅读目录 介绍Weinre 安装Weinre 通过Weinre调试页面 启动target 介绍Weinre Weinre(Web Inspector Remote),是一种远程调试工具.功能与Fire ...
- material.setTexture("sampler",tex) assetbundle 下失效
做镜面反射本来写很顺 在手机上测的时候 发现settexture这里绑不上 查好久 是assetbundle的缘故 因为动态加载的 obj用了mat01 我在反射脚本里动态修改mat01而不是拿 re ...
- 【树莓派】Box相关手册-4 Web代理
在盒子上安装配置web代理,通过此代理,用户访问盒子的内网地址实现web portal访问.目前我们使用tinyproxy作为web代理 安装: >sudo apt-get install ti ...
- 解决安装完Ubuntu系统后启动项中没有Ubuntu的问题
问题出现的原因是你没有把grub安装到硬盘的起始扇区里,按理说Ubuntu在安装的时候应该能很好的处理这个问题,但有个别电脑还是会出问题.不过我们可以通用命令解决 问题. 使用U盘进入Ubuntu系统 ...
- Rxjava2.0 链式请求异常处理
使用Rxjava2.0的过程中,难免会遇到链式请求,而链式请求一般都是第一个抛异常,那么后面的请求都是不会走的.现在来讨论一下链式请求的一种异常处理方法.例如: 一个登录-->通过登录返回的to ...
- [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读
---------------------------------------------------------------------------------------------------- ...