BFS(两点搜索) UVA 11624 Fire!
题目传送门
1 /* 2 BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界。 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 8:11:54 7 File Name :UVA_11624.cpp 8 *************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 #include <iostream> 13 #include <sstream> 14 #include <cstring> 15 #include <cmath> 16 #include <string> 17 #include <vector> 18 #include <queue> 19 #include <deque> 20 #include <stack> 21 #include <list> 22 #include <map> 23 #include <set> 24 #include <bitset> 25 #include <cstdlib> 26 #include <ctime> 27 using namespace std; 28 29 #define lson l, mid, rt << 1 30 #define rson mid + 1, r, rt << 1 | 1 31 typedef long long ll; 32 const int MAXN = 1e3 + 10; 33 const int INF = 0x3f3f3f3f; 34 const int MOD = 1e9 + 7; 35 char maze[MAXN][MAXN]; 36 bool vis[MAXN][MAXN]; 37 int step[MAXN][MAXN]; 38 int dx[4] = {-1, 1, 0, 0}; 39 int dy[4] = {0, 0, -1, 1}; 40 int n, m; 41 42 bool judge_f(int x, int y) { 43 if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] == '#') return false; 44 return true; 45 } 46 47 bool judge_j(int x, int y) { 48 if (x < 1 || x > n || y < 1 || y > m) return true; 49 return false; 50 } 51 52 void BFS_F(void) { 53 memset (step, INF, sizeof (step)); 54 memset (vis, false, sizeof (vis)); 55 queue<pair<int, int> > Q; 56 for (int i=1; i<=n; ++i) { 57 for (int j=1; j<=m; ++j) { 58 if (maze[i][j] == 'F') { 59 Q.push (make_pair (i, j)); vis[i][j] = true; 60 step[i][j] = 0; 61 } 62 } 63 } 64 while (!Q.empty ()) { 65 int x = Q.front ().first, y = Q.front ().second; Q.pop (); 66 for (int i=0; i<4; ++i) { 67 int tx = x + dx[i], ty = y + dy[i]; 68 if (!judge_f (tx, ty)) continue; 69 step[tx][ty] = step[x][y] + 1; 70 Q.push (make_pair (tx, ty)); vis[tx][ty] = true; 71 } 72 } 73 } 74 75 void BFS_J(void) { 76 memset (vis, false, sizeof (vis)); 77 queue<pair<int, int> > Q; 78 for (int i=1; i<=n; ++i) { 79 for (int j=1; j<=m; ++j) { 80 if (maze[i][j] == 'J') { 81 Q.push (make_pair (i, j)); step[i][j] = 0; vis[i][j] = true; break; 82 } 83 } 84 } 85 while (!Q.empty ()) { 86 int x = Q.front ().first, y = Q.front ().second; Q.pop (); 87 for (int i=0; i<4; ++i) { 88 int tx = x + dx[i], ty = y + dy[i]; 89 if (judge_j (tx, ty)) { 90 printf ("%d\n", step[x][y] + 1); return ; 91 } 92 if (step[x][y] + 1 >= step[tx][ty] || vis[tx][ty] || maze[tx][ty] == '#') continue; 93 Q.push (make_pair (tx, ty)); step[tx][ty] = step[x][y] + 1; vis[tx][ty] = true; 94 } 95 } 96 puts ("IMPOSSIBLE"); 97 } 98 99 int main(void) { //UVA 11624 Fire! 100 int T; scanf ("%d", &T); 101 while (T--) { 102 scanf ("%d%d", &n, &m); 103 for (int i=1; i<=n; ++i) { 104 scanf ("%s", maze[i] + 1); 105 } 106 BFS_F (); BFS_J (); 107 } 108 109 return 0; 110 }
转载于:https://www.cnblogs.com/Running-Time/p/4703122.html
BFS(两点搜索) UVA 11624 Fire!相关推荐
- UVA - 11624 Fire! 两次BFS
UVA - 11624 Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and ...
- 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】
POJ3126Prime Path 给定两个四位素数a b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位 ...
- UVA - 11624 Fire!
题意:求Joe走出迷宫的最短时间,有障碍,还有向四周蔓延的火 思路:将火的位置也加进BFS里面 #include <iostream> #include <cstdio> #i ...
- UVA - 11624 - Fire!
题目描述: 小明最后也没能进入游戏大厂,也没能娶到心爱的女孩,现在小明在一家迷宫里工作. 不幸的是,迷宫里因为线路老化而发生了火灾.小明现在需要一个逃跑路线,请你帮助倒霉的小明从迷宫中逃离出去吧 小明 ...
- Fire! UVA - 11624
题目链接:Fire! UVA - 11624 =================================================== Fire Time Limit: 1000MS D ...
- J - Fire! UVA - 11624
J - Fire! UVA - 11624 题意:火每次能烧到上下左右,人碰到非墙的边界则逃火成功,求最短的逃离时间. 由于bfs每个位置最多入队出队一次,所以复杂度为 1e6 一发bfs直接TLE, ...
- LeetCode-笔记-199. 二叉树的右视图——BFS广度优先搜索
LeetCode-笔记-199. 二叉树的右视图 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,n ...
- 图:BFS(深度优先搜索)图解分析代码实现
文章目录 一.介绍 二.图的建立 2.1建立图类 2.2建立图 三.BFS 3.1图解: 3.2代码 四.DFS和BFS完整代码 一.介绍 图的DFS(深度优先搜索)与BFS(广度优先搜索)是图的两种 ...
- BFS简单搜索--POJ 2243
这题就是简单的BFS搜索,刚刚转到C++,还有很多库函数不熟悉,理解到BFS是一种奇妙的迭代法,其用的主要是队列的性质. 1 /*BFS简单搜索*/ 2 #include<iostream> ...
最新文章
- shell学习之-sed用法解析_【Linux】shell学习之sed
- nextpolish安装_「三代组装」使用Pilon对基因组进行polish
- 临床医学与计算机考研哪个好考,临床医学考研院校难度概况
- 做一个.net 程序员要掌握的知识提纲
- 网络工程属于计算机哪一类,网络工程专业属于什么门类
- C++11并发实战(专栏)
- 企业如何采用机器学习
- Anaconda安装找不到的依赖包,以DBUtils包为例
- ZStack常见问题
- rust : rustup切换stable、nightly
- Struts框架可以支持以下哪种程序开发语言?(选择1项)
- 尚硅谷大数据Spark之RDD转换算子学习笔记及面试题
- 解决谷歌浏览器自动填充表单
- 郑大第九届校赛正式赛(1819: 加加加!(油))
- Slices in Python
- 创建mysql数据库快照_sql语句大全之创建数据库快照
- github标准pull request提交流程
- 《电子元器件的可靠性》——2.4节电子元器件的失效规律
- java笔记——反射
- perp系列之四:perp下载
热门文章
- ubuntu16.04 打开chrome弹出“Enter password to unlock your login keyring”解决方法
- Promise--优雅的异步回调解决方案
- 解决 HomeBrew 下载缓慢的问题
- 移动端高清适配方案(解决图片模糊问题、1px细线问题)
- 解决chrome浏览器adobe flash player不是最新版本亲测可用的方法
- 在Rails资产管道中使用字体
- idea安装配置tomcat
- 显示表格数据网页php源码,网页上可以复制的表格数据,为什么察看源代码找不到这些数据?_html/css_WEB-ITnose...
- 电脑遇到脱机状态怎么解除?
- html5 js阻塞加载,js无阻塞加载和defer、async详解_白峰_前端开发者