UVA 11624 Fire!(广度优先搜索)
题目大意:在一个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!(广度优先搜索)的更多相关文章
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
随机推荐
- ef执行记录原生态代码方法。
select e; var f = (System.Data.Objects.ObjectQuery<SimpleEntry>)final; var s = f.ToTraceString ...
- Spring AOP 简单理解
AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程.核心是在不增加代码的基础上,还增加了新的功能.AOP编程在开发框架本身用的比较多,而实际项目中,用的 ...
- python学习-day14:集合,函数,格式化
一.集合 定义:由不同元素组成的集合.集合是一组无序排列的可hash值, 可以作为字典的key.元素必须是不可变类型:只能存放数字,字符串,字典 特性:集合的目的是将不同的值放在一起,不同的集合之间可 ...
- Gson处理
public class GsonTools { public GsonTools(){} public static <T> T getPerson(String jsonString, ...
- UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)
UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...
- UML精粹1 - 简介
Martin的主页 http://martinfowler.com/. Pavel Hruby开发的visio模板,可以用来画UML图: http://phruby.com 简介 统一建模语言UML是 ...
- EditText----
==============01 editText属性 1.输入法Enter键图标的设置: 软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,a ...
- mycat 插入语句导致的一个Dobbo问题
2017-01-03 11:11:52.621 [com.alib] (): [DUBBO] Send heartbeat to remote channel /121.43.177.8:20192, ...
- IIS7 rename application or site
rename site 比较容易,在IIS里面就可以直接 rename,也可以用 cmd 的方式 1. 打开 cmd 2. cd C:\Windows\System32\inetsrv 3. appc ...
- 解决Mac下GDB提示签名错误
http://blog.csdn.net/powerlly/article/details/30323015