UVA 11642 Fire!
Fire!
This problem will be judged on UVA. Original ID: 11624
64-bit integer IO format: %lld Java class name: Main
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 解题:bfs...让火先走,再人走。。判断走出去的是人还是火
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int n,m;
struct node{
int x,y,t;
bool isFire;
node(int a = ,int b = ,int c = ,bool isfire = true){
x = a;
y = b;
t = c;
isFire = isfire;
}
};
queue<node>q;
bool isIn(int x,int y){
return x < n && x >= && y < m && y >= ;
}
int bfs(){
static const int dir[][] = {-,,,,,-,,};
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = ; i < ; ++i){
int tx = now.x + dir[i][];
int ty = now.y + dir[i][];
if(isIn(tx,ty)){
if(!vis[tx][ty]){
vis[tx][ty] = true;
q.push(node(tx,ty,now.t+,now.isFire));
}
}else if(!now.isFire) return now.t+;
}
}
return -;
}
int main(){
int kase,px,py;
scanf("%d",&kase);
while(kase--){
scanf("%d %d",&n,&m);
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
for(int i = ; i < n; ++i){
scanf("%s",mp[i]);
for(int j = ; j < m; ++j){
if(mp[i][j] == 'F'){
vis[i][j] = true;
q.push(node(i,j,,true));
}else if(mp[i][j] == '#') vis[i][j] = true;
else if(mp[i][j] == 'J') vis[px = i][py = j] = true;
}
}
q.push(node(px,py,,false));
int ans = bfs();
if(ans == -) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
return ;
}
/*
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F */
UVA 11642 Fire!的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
随机推荐
- HNU13377:Book Club(DFS)
Problem description Porto's book club is buzzing with excitement for the annual book exchange event! ...
- Lambda表达式-使用说明
jdk8已经发布4年,其中有一个特性:Lambda,它是一个令开发者便捷开发的一种方式,Lambda Expression (Lambda表达式)是为了让java提供一种面向函数编程,原本在jdk8之 ...
- 网络流Dinic算法模板 POJ1273
这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...
- Metasploit的armitage初步使用
armitage的启动 root@kali:~# armitage 别急,过会儿就好了 . 等扫描完会弹出一个框框然后会多出目标的图标比如目标是打印机
- Java基础学习(六)-- 递归以及文件I/O流基础详解
递归 1.递归的概念: 在函数自身内部,调用函数本身的方式,称为递归. 2.递归的注意事项:包括递进去,归出来两步. 即:首先依次执行[函数调自身语句]上半部分的代码,知道最里层.(递进去),然后 ...
- Android项目实战(五十七):Glide 高斯模糊效果
核心需要高斯模糊的库 compile 'jp.wasabeef:glide-transformations:2.0.1' 针对于3.7的版本 使用方法为: //加载背景, Glide.with(Mus ...
- 安装vue时使用npm install 报错
npm ERR! Darwin 14.3.0 npm ERR! argv "/usr/local/Cellar/node/6.4.0/bin/node" "/usr/lo ...
- grvphviz && dot脚本语言
安装graphviz 可去官网下载http://www.graphviz.org/download/下载之后按步骤安装 打开编辑器,创建*.dot文件,编辑dot脚本代码,保存. D:\>dot ...
- 归档备份被删,GoldenGate无法抽取数据
发生错误如下,源端EXTRACT进程异常中止,查看日志,发现如下错误. 2014-07-23 01:32:13 ERROR OGG-00446 Oracle GoldenGate Captur ...
- yii2.0缓存篇之片段缓存
片段缓存指的是缓存页面内容中的某个片段.默认缓存 60秒. return $this->renderPartial("ca"); ...