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次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
随机推荐
- Java构造和解析Json数据的两种方法详解二——org.json
转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html 在www.json.org上公布了很多JAVA下的jso ...
- windows2012 IIS部署GeoTrust证书踩过的坑。
系统:windows2012 环境:IIS8 在阿里云上买了GeoTrust证书, 按照说明下载证书到服务器, 导入证书, 给IIS站点部署https. 阿里云部署帮助文档:https://help ...
- zigbee 学习笔记
在德州仪器的站点:http://www.ti.com.cn/tool/cn/z-stack上下载安装zigbee2007协议栈版,我的是ZStack-CC2530-2.3.0-1.4.0. 以下演示一 ...
- iOS:iOS开发系列–打造自己的“美图秀秀”(下)
来源: KenshinCui 链接:http://www.cnblogs.com/kenshincui/p/3959951.html 运行效果: 其他图形上下文 前面我们也说过,Quartz 2D的图 ...
- iOS:详解MJRefresh刷新加载更多数据的第三方库
原文链接:http://www.ios122.com/2015/08/mjrefresh/ 简介 MJRefresh这个第三方库是李明杰老师的杰作,这个框架帮助我们程序员减轻了超级多的麻烦,节约了开发 ...
- piwik网站访问统计系统
一.Piwik介绍 Piwik是一套基于PHP+MySQL技术构建的开源网站访问统计系统.Piwik可以给你详细的统计信息,比如网页浏览人数,访问最多的页面,搜索引擎关键词等流量分析功能.此外,它还采 ...
- 新型数据库Kudu应用经验分享
小米使用kudu的案例 http://www.aiweibang.com/yuedu/60603532.html 调研kudu的情况
- 十个书写Node.js REST API的最佳实践(下)
收录待用,修改转载已取得腾讯云授权 5. 对你的Node.js REST API进行黑盒测试 测试你的REST API最好的方法之一就是把它们当成黑盒对待. 黑盒测试是一种测试方法,通过这种方法无需知 ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- freemarker的list指令小技术归纳
1.问题:当数据超过3位的时候,freemarker会自动用逗号截取,例如2,311 解决方法(一种即可): (1)加.toString(),如:${(data).toString()} (2)加?c ...