题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到

之前逃出迷宫,若能逃出输出最短时间。很明显的bfs。但由于火到达的地方人不能抵达,故需先对火进行bfs,标记后若人在火烧到之前抵达即可。最后逃出时间需要加一,

因为当时只是抵达边界,若逃出时间需加一。

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
#define oo 0x3f3f3f3f
int dir[][] = {{, }, {, -}, {, }, {-, }};
int m, n, time[][], vis[][];
char maps[][];
struct point
{
int x, y, step;
};
point start;
queue<point>Fire;
void Fire_time()
{
point now, next;
while(Fire.size())
{
now = Fire.front();
Fire.pop();
time[now.x][now.y] = now.step;
for(int i=; i<; i++)
{
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
next.step = now.step + ;
if(next.x>= && next.x<m && next.y>= && next.y<n && !vis[next.x][next.y]
&& maps[next.x][next.y]!='#')
{
vis[next.x][next.y] = ;
Fire.push(next);
}
}
}
}
int bfs()
{
queue<point>Q;
Q.push(start);
memset(time, oo, sizeof(time));
Fire_time();
memset(vis, , sizeof(vis));
point now, next;
while(Q.size())
{
now = Q.front();
Q.pop();
if(now.x==m- || now.y==n- || now.x== || now.y==)return now.step+;
for(int i=; i<; i++)
{
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
next.step = now.step + ;
if(next.x>= && next.x<m && next.y>= && next.y<n && !vis[next.x][next.y]
&& maps[next.x][next.y]=='.' && next.step<time[next.x][next.y])
{
vis[next.x][next.y] = ;
Q.push(next);
}
}
}
return -;
}
int main()
{
int Text;
scanf("%d", &Text);
while(Text--)
{
scanf("%d %d", &m, &n);
memset(vis, , sizeof(vis));
for(int i=; i<m; i++)
scanf("%s", maps[i]);
for(int i=; i<m; i++)
for(int j=; j<n; j++)
{
if(maps[i][j]=='J')
{
start.x = i;
start.y = j;
start.step = ;
}
if(maps[i][j]=='F')
{
point s;
s.x = i;
s.y = j;
s.step = ;
Fire.push(s);
vis[i][j] = ;
}
}
int ans = bfs();
if(ans==-)printf("IMPOSSIBLE\n");
else printf("%d\n", ans);
}
return ;
}

UVA 11624 Fire!(广度优先搜索)的更多相关文章

  1. uva 11624 Fire!(搜索)

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

  2. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

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

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

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

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

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

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

  6. UVA 11624 Fire!【两点BFS】

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

  7. UVA 11624 - Fire! 图BFS

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

  8. UVa 11624 Fire!(BFS)

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

  9. UVA 11624 Fire! (bfs)

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

随机推荐

  1. 如何有效的使用C#读取文件

    如何有效的使用C#读取文件  你平时是怎么读取文件的?使用流读取.是的没错,C#给我们提供了非常强大的类库(又一次吹捧了.NET一番),里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件 ...

  2. 入口点函数的19种消息,AcRxArxApp只处理16种。

    AcRx::AppMsgCode一共有19种消息. 但由IMPLEMENT_ARX_ENTRYPOINT宏实现的App类,只处理了16种消息. 缺: kSuspendMsg = 16,    kIni ...

  3. 【python】进程

    multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...

  4. NSIS使用记录

    ; 该脚本使用 HM VNISEdit 脚本编辑器向导产生 ; 安装程序初始定义常量 !define PRODUCT_NAME "" !define PRODUCT_VERSION ...

  5. C#禁止程序重复启动

    采用线程互斥锁Mutex,在winform程序的主入口点中加入如下代码,将程序改为单实例运行. static class Program { /// <summary> /// 应用程序的 ...

  6. 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1

    在Windows Server 2008下直接安装SQL Server 2008时,会出现如下错误: 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1 ...

  7. 随笔 高质量 C++/C 编程指南

    内存分配方式有三种:) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量, static 变量.) 在栈上创建.在执行函数时,函数内局部变量的存储 ...

  8. 【cl】Json学习

    http://www.cnblogs.com/java-pan/archive/2012/04/07/2436507.html

  9. QueryRunner类

    该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量. QueryRunner类提供了两个构造方法: 默认的构造方法 需要一个 ja ...

  10. Flex 加载tiff

    gis系统常常要加载tiff,因为好多土地证书,各种文件都是扫描件,如果你是用as来写的前台,怎么加载呢,顺便说下用插件AlternaTIFF也是可以得不过浏览器加载这么多插件是不太好的. 首先TIF ...