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次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
随机推荐
- phpMyAdmin配置
一: (1):下载phpmyadmin,在官方最好 (2):个人建议最好将其安装在apache的htdocs文件中(apache的默认虚拟目录,或web访问目录) ...
- HTTP代理神器Fiddler
HTTP代理神器Fiddler Fiddler是一款强大Web调试工具,它能记录所有客户端和服务器的HTTP请求. Fiddler启动的时候,默认IE的代理设为了127.0.0.1:8888,而其他浏 ...
- appium-desktop使用
Appium移动测试中有个很重新的组件Appium-Server,它主要用来监听我们的移动设备(真机或模拟器),然后将不同编程语言编写的 appium 测试脚本进行解析,然后,驱动移动设备来运行测试. ...
- 约瑟夫环形链表问题、丢手帕问题、剑指offer圆圈中最后一个数问题
public class Solution { // 左神解法,本题本质还是报数为m-1的倍数的人死.求最后一个活着的人是初始时候的哪个人 /* 报数(A) 实际人员编号(B) ...
- Win7如何关闭 打开文件-安全警告
如图所示,运行一个EXE程序就会弹出提示,很麻烦. 在运行对话框中输入gpedit.msc打开组策略编辑器.定位到用户配置--管理模板--windows组件--附件管理器 点中等危险文件类型抱含列 ...
- ResourceBundle的使用
ResourceBundle用来读取properties配置文件,配置文件的位置只能放到src根目录下,当然这个功能的目的是为了实现国际化. 代码如下: package com.comp.common ...
- ios开发杂项(基础性介绍等)
IOS Xcode开发中的文件后缀名区别m,mm,cpp,h .h :头文件.头文件包含类,类型,函数和常数的声明. .m :源代码文件.这是典型的源代码文件扩展名,可以包含Objective-C和C ...
- angular -- $resource 用法
安装 ngResource模块是一个可选的angularjs模块,如果需要使用,我们要单独引用js <script type="text/javascript" src=&q ...
- Java之内部类(1) - 为什么需要内部类
为什么需要内部类 一般来说,内部类继承自某个类或实现某个接口,内部类的代码操作创建它的外围类的对象.所以可以认为内部类提供了某种进入其外围类的窗口. 内部类必须要回答的一个问题是:如果只是需要一个对接 ...
- java数据库编程:使用元数据分析数据库
databaseMetaData 使用元数据可以分析数据库基本信息,包括版本,数据库名称,或者指定表的主键. 代码实例 package 类集; import java.sql.Connection ; ...