题目大意:在一个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. 黄聪:Wordpress二级域名共享用户cookie出现错误解决方案及WP的Cookie机制

    在若干年以前,我刚开始折腾Wordpress没多久的时候,就自己摸索过 多个Wordpress网站共享一份数据表的实现方法 .这种看起来好像很高大上的类SSO功能,能够给用户在多个网站之间提供快速.无 ...

  2. webpack +vue开发(2)

    我们的loader方式其实可以写成inline的方式 loaders:[ { test:/\.js$/, loader:"babel", exclude:/node_modules ...

  3. 两个二进制数多少个位(bit)不同

    class Solution { public: /** * 获得两个整形二进制表达位数不同的数量 * * @param m 整数m * @param n 整数n * @return 整型 */ in ...

  4. 【javascript基础】3、变量和作用域

    前言 这篇和大家说一下javascript中的变量和作用域,由于是将基础嘛,主要给大家捋一下知识,不想翻开书复习的道友可以看一下,打算刚开始学习javascript的同学可以扫一眼. PS:jQuer ...

  5. Json操作(DynamicJson)

    Json的简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了 ...

  6. css学习笔记(5)

    <style>*{margin:0; padding:0;}ul{ list-style:none;}li{ height:30px; width:100px; background:#F ...

  7. NPOI生成单元格(列)下拉框

    客户提出能否将导入模板中,课程一列添加下拉框方便选择,不用手输入,以减少输入错误的可能性.于是在网上找了点代码,稍加整理后,形成了以下方案,代码部分: 一:生成课程列表,并放置在excel的单独she ...

  8. 我的ZJ解题心得

    想要学好程序设计第一是要养成你的编程思维,也就是你对编程的一种概念和思维定式,长期的解题会让你产生解题经验进而形成一种思维定式,比如看到一个题目就立即想出这题要用什么方法解题这样.编程思维我认为还包括 ...

  9. 利用jQuery和Ajax实现检测用户名是否已经被注册

    这是一个jQuery和Ajax的很基础的应用,是我出去面试时的一个面试题.当时脑子有点懵想了好久才知道该怎么去实现,现在回来再看了下书好好总结一下这个东西. 首先新建一个html文件,只有简单的几行代 ...

  10. Python基础篇【第2篇】: Python内置函数(一)

    Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...