题目大意:在一个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. angular+bootstrap+MVC 之三,分页控件初级版

    今天实现一个分页控件,效果如下: 1.HTML: <!doctype html> <!--suppress ALL --> <html ng-app="appT ...

  2. python(25)下载文件

    利用程序自己编写下载文件挺有意思的.Python中最流行的方法就是通过Http利用urllib或者urllib2模块.当然你也可以利用ftplib从ftp站点下载文件.此外Python还提供了另外一种 ...

  3. web页面打开本地app(判断是否安装)

    在应用宝中有APP申请链接: //是否可以打开App不可以跳则到下载页 $(".downNow button").on("click",function(){ ...

  4. Spring AOP 完成日志记录

    Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046

  5. java 线程的命名

    //线程的命名 class xc2 extends Thread{ public void run(){ for(int i=0;i<20;i++){ //Thread.currentThrea ...

  6. cocos2dx3.0 removeFromParent和removeAllChildren含义

    顾名思义,removeFromParent就是把自己从父亲那里移除,removeAllChildren就是移除自己所有的孩子,这些方法的具体实现都在基类Node里面,通过查看代码也很容易看到怎么实现的 ...

  7. 【caffe-windows】 caffe-master 之 matlab接口配置

    平台环境: win10 64位 caffe-master  vs2013 Matlab2016a 第一步: 打开\caffe-master\windows下的CommonSettings.props文 ...

  8. 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)

    Java time JavaScript Math.round(new Date().getTime()/1000)getTime()返回数值的单位是毫秒 Microsoft .NET / C# ep ...

  9. 关于新闻,在线编辑器建表时此字段一定要为text

    create table about( content text )engine=myisam default charset=utf8; 项目的各个建表语句 create database day4 ...

  10. delphi action学习

    procedure TEditAction.UpdateTarget(Target: TObject); begin if (Self is TEditCut) then Enabled := (Ge ...