Fire!

Time Limit: 1000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 11624
64-bit integer IO format: %lld      Java class name: Main

 
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 解题: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!的更多相关文章

  1. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  2. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  3. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  4. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  5. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

  6. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  7. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

  8. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  9. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

随机推荐

  1. java webproject中logback换配置文件的路径

    本人小站点:   http://51kxd.com/  欢迎大家不开心的时候訪问訪问,调节一下心情 web.xml中配置: <!-- windows  logback.xml文件跟web容器(比 ...

  2. 一站式学习WireShark

    一 基础 http://blog.sina.com.cn/s/blog_987e00020102x5k1.html 选中一个数据包,右键选中某一个数据包,然后follow-->Tcp Strea ...

  3. 图像几何变换(geometric transformation)

    1. imwarp B = imwarp(A,tform) demo I = imread('cameraman.tif'); tform = affine2d([1 0 0; .5 1 0; 0 0 ...

  4. windows下git的安装和使用

    git到底是个什么东西,我这里就不介绍了,如果大家还有不懂的,可以去百度一下的.我这里给一个介绍的网址:git简介        这里在留一个地址http://baike.baidu.com/subv ...

  5. Codeforces 708D 费用流 (呃我们的考试题)

    NB的题目背景 输入输出一样 考试的时候貌似只有gzz一个人搞出来了 %gzz 思路: 分情况讨论 add(x,y,C,E) C是费用 E是流量 1. f>c add(x,y,2,inf),ad ...

  6. codeforces 404 B Marathon【fmod对浮点数取余】

    题意:给出一个边长为a的正方形,给出d,给出n,输出走得距离为i个d的时候的坐标 学习的这一篇 http://blog.csdn.net/synapse7/article/details/215956 ...

  7. BZOJ 2724 [Violet 6]蒲公英(分块)

    题意 在线区间众数 思路 预处理出 f[i][j] 即从第 i 块到第 j 块的答案.对于每个询问,中间的整块直接用预处理出的,两端的 sqrtn 级别的数暴力做,用二分查找它们出现的次数.每次询问的 ...

  8. 开源映射平台Mapzen加入了Linux基金会的项目

    2019年1月29日,Linux基金会宣布,开源映射平台Mapzen现在是Linux基金会项目的一部分. Mapzen专注于地图显示的核心组件,如搜索和导航.它为开发人员提供了易于访问的开放软件和数据 ...

  9. 使用 swoole_process 实现 PHP 进程池

    swoole_process 主要是用来代替 PHP 的 pcntl 扩展.我们知道 pcntl 是用来进行多进程编程的,而 pcntl 只提供了 fork 这样原始的接口,容易使用错误,并且没有提供 ...

  10. STM32中断名词

    1.NVIC的优先级概念    占先式优先级 (pre-emption priority):    高占先式优先级的中断事件会打断当前的主程序/中断程序运行— —抢断式优先响应,俗称中断嵌套.    ...