/*************************************************************************
> File Name: test.cpp
> Author: HJZ
> Mail: 2570230521@qq.com
> Created Time: 2014年08月03日 星期日 07时26分58秒
************************************************************************/ /*
题目大意:一个人joe想从迷宫中逃脱,但是迷宫中有着火的地方!joe每分钟向上下左右其中一个方向走一步,当然有火的地方和有墙的地方是不能通过的!
另外,火的蔓延的方向是同一时刻向上下左右四个方向蔓延! 思路:每一时刻,先将火的蔓延位置标记出来,并将新的火的位置放入队列qf中;
因为某一时刻,我们将所有joe可能走的路径都放入了队列中了,假如t1时刻他能走的位置是5个,那么在t1+1时刻,根据上一时刻t1的可能位置更新t1+1
时刻的可能位置,t1时刻的位置出队列q, t1+1时刻的新位置并重新进入队列!
*/ #include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include<cmath>
#include <algorithm>
#include<queue>
#define M 1005
#define mem(a) (memset((a), 0, sizeof(a)))
#define get(s) fgets(s, sizeof(s)-1, stdin) using namespace std; char map[M][M];
int n, m;
int bg, ed;
int dir[][]={, , , , -, , , -}; struct Point{
int x, y, step;
Point(){ }
Point(int x, int y, int step){
this->x=x;
this->y=y;
this->step=step;
}
};
queue<Point>q; queue<Point>qf;
int cntF;//某一时刻,火点的位置进入队列的个数
int cntP;//某一时刻,joe可走位置进入队列的个数 int bfs(){
while(!q.empty()) q.pop();
q.push(Point(bg, ed, ));
while(){
while(!qf.empty() && cntF){
Point Fcur=qf.front();
qf.pop();
--cntF;
int x=Fcur.x, y=Fcur.y;
for(int i=; i<; ++i){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(map[xx][yy]!='F' && map[xx][yy]!='#'){
map[xx][yy]='F';
qf.push(Point(xx, yy, ));
}
}
}
cntF=qf.size();
while(!q.empty() && cntP){
Point cur=q.front();
q.pop(); --cntP; int x=cur.x, y=cur.y;
if(x== || x==n || y== || y==m) return cur.step;
for(int i=; i<; ++i){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(map[xx][yy]!='#' && map[xx][yy]!='F' && map[xx][yy]!='X'){
map[xx][yy]='X';
if(x== || x==n || y== || y==m) return cur.step+;
q.push(Point(xx, yy, cur.step+)); } } }
cntP=q.size();
if(cntP==) return -;
}
return -;
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(int i=; i<=n+; ++i)
map[i][]=map[i][m+]='#';
for(int i=; i<=m+; ++i)
map[][i]=map[n+][i]='#'; while(!qf.empty()) qf.pop();
cntF=;
cntP=;
for(int j=, i=; i<=n; ++i){
scanf("%s", map[i]+);
for(j=; j<=m; ++j)
if(map[i][j]=='J'){
bg=i;
ed=j;
}
else if(map[i][j]=='F'){
++cntF;
qf.push(Point(i, j, ));
}
map[i][j]='#';
} int tt=bfs();
if(tt!=-)
printf("%d\n", tt);
else printf("IMPOSSIBLE\n");
}
return ;
}

Uvaoj 11624 - 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!(广度优先搜索)

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

  5. UVa 11624 Fire!(BFS)

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

  6. UVA 11624 Fire! (bfs)

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

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

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

  8. UVA 11624 Fire!【两点BFS】

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

  9. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

随机推荐

  1. 插入排序-java

    排序-插入排序 基本思想:将待排序表看作左右两部分,其中左边为有序区,右边为无序区, 整个排序过程就是将右边无序区中的元素逐个插入到左边的有序区中,以构成新的有序区. 平均时间:O(n2) 最好情况: ...

  2. SpringMVC入门配置和简单实现

    web.xml的配置 <!-- springmvc中央控制器 --> <servlet> <servlet-name>springmvc</servlet-n ...

  3. Android--数据解析

    一.XML解析: 1.Pull 解析方式: 1)先获取到一个 XmlPullParserFactory 的实例, 并借助这个实例得到 XmlPullParser 对象: 2)调用 XmlPullPar ...

  4. Ubuntu 12.04 改造指南

    文章转自:http://www.lupaworld.com/article-217719-1.html 升级12.04已经有一段时间了.作为一个从08年就开始用Ubuntu的老用户,我觉得作为一个LT ...

  5. windows配置php开发环境

    1.安装xampp. xampp集成了php.prel.mysql.apache等网站工具,安装超简单,本身也超级好用.点击下载xampp 2.讲xmapp中的php配置到环境变量 比如我的xampp ...

  6. 2013-09-22 [随笔]-Roy

    不能因为一些小事情而一直影响自己的心情. 每天过得都应该有重点,无论是家庭还是工作还是其他. 要多花时间去吸取些新东西,看书,丰富自己的想法. 不能让日常的乱七八糟影响心情. bingo!

  7. windows下在文件夹中快速启动cmd

    在windows下,有时候由于特定需要,我们经常需要将cmd  cd到某文件下进行命令行操作,其实,这里有一个小技巧: 在一个文件下,按住shift键,然后点击鼠标右键,你就会发现菜单栏中多出一个“在 ...

  8. 循序渐进做项目系列(1):最简单的C/S程序——让服务器来做加法

    (本文是专门针对未接触过C/S开发的初学者而写的,C/S开发高手请自动忽略啊~~) 还在写“Hello world!”式的单机程序吗?还在各种拖控件吗?是否自己都觉得有点low呢?来个质的飞跃吧!看看 ...

  9. 【读书笔记】Ninject 在MVC5中的使用

    从MVC3中就开始接触Ninject这个IOC工具.也一直是MVC Framework系列书籍中推荐的IOC工具,当然还有优秀的Autofac等.性能和使用上面个有千秋.下面先看一下Ninject的使 ...

  10. JavaScript包装对象

    JavaScript是面向对象的语言,使用”.”操作符可以访问对象的属性和方法,而对于基本类型(null, undefined, bool, number, string)应该是值类型,没有属性和方法 ...