题目大意:在一个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. mac idea 设置

    鼠标悬停显示文档注释:preferences->Editor->General:勾选 show quick documentation on mouse move 智能提示模糊搜索:pre ...

  2. <s:select>下拉框是空白的解决办法

    首先,定义了一个Department的JavaBean对象如下 DAO.java 在一个Action类Employee.java中将depts放入requestMap中 最后,在index.jsp里定 ...

  3. ruby md5加签验签方法

    # md5签名def md5_sign(data,key) return OpenSSL::Digest::MD5.hexdigest(data+key)end # md5验签def md5_veri ...

  4. Hadoop学习13--zookeeper相关

    zookeeper要保证各个server之间同步,实现同步的协议是zab协议.此协议有两种模式:恢复模式(选主)和广播模式(同步). 服务启动或者leader崩溃时,进入恢复模式.选举成功且大多数se ...

  5. Centos6.4 用rpm方式安装MySql5.6

    1.查看系统是否安装了MySQL     使用命令:     #rpm -qa | grep mysql    2.卸载已安装的MySQL      卸载mysql命令如下:       #rpm - ...

  6. Chrome 调试动态加载的js

    今天有个同事问到我用chrome调试动态加载js的问题,这个问题之前遇到过,只是时间有点长了,有些忘记.在这里做一下记录: 在要调试的源码的后面加上 //@ sourceURL= debug.js 注 ...

  7. log4net 发布到生产环境不写日志的解决方法--使用 NLog日志

    1.升级到log4net的最新版 PM下执行 Install-Package log4net 还是无法解决的,使用下面的方法 2.使用Nlog替换之,详见https://github.com/NLog ...

  8. Unity3D学习笔记

    双击或F-居中显示对象 Alt-旋转场景 Align With View-正视主镜头 添加质量 使成为预制物体, 即flash中元件, 预制物体在Hierarchy中名字成蓝色, Assets是的对象 ...

  9. (转载)SQL Server 2005 如何启用xp_cmdshell组件

    原文地址:http://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html [错误描述]: SQL Server阻止了对组件‘xp_cmdshell ...

  10. 66. Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...