hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了。
题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1)。
'X'为墙,'.'为路,数字为怪物。墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间。
比较麻烦的是需要记录路径。还要记录是在走路还是在打怪。
因为求最短路,所以可以使用bfs。
因为进过每一个点花费时间不同,所以可以使用优先队列。
因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点。当然,记录方法不止一种。
上代码——
1 #include <cstdio> 2 #include <cstring> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 9 struct node 10 { 11 int x, y, step; 12 bool operator < (const node& a) const 13 { 14 return a.step < step; 15 } 16 }; 17 18 int go[4][2] = {{1, 0},{-1, 0}, {0, 1}, {0, -1}}; 19 20 int n, m, step; 21 bool v[110][110]; 22 char mp[110][110]; 23 int last[110][110]; 24 25 bool bfs() 26 { 27 node p, q; 28 p.x = n-1; 29 p.y = m-1; 30 p.step = 0; 31 if(mp[n-1][m-1] >= '0' && mp[n-1][m-1] <= '9') p.step += mp[n-1][m-1] -'0'; 32 33 priority_queue <node> que; 34 if(mp[p.x][p.y] != 'X') 35 que.push(p); 36 v[p.x][p.y] = 1; 37 38 while(!que.empty()) 39 { 40 p = que.top(); 41 que.pop(); 42 43 for(int i = 0; i < 4; i++) 44 { 45 int x = p.x+go[i][0]; 46 int y = p.y+go[i][1]; 47 48 if(x >= 0 && x < n && y >= 0 && y < m && !v[x][y]) 49 { 50 if(mp[x][y] == 'X') continue; 51 52 q.x = x; q.y = y; q.step = p.step+1; 53 if(mp[x][y] >= '1' && mp[x][y] <= '9') q.step += mp[x][y]-'0'; 54 que.push(q); 55 v[x][y] = 1; 56 last[x][y] = p.x*1000+p.y; 57 if(x == 0 && y == 0) {step = q.step; return 1;} 58 59 } 60 } 61 } 62 return 0; 63 } 64 65 void output() 66 { 67 printf("It takes %d seconds to reach the target position, let me show you the way.\n", step); 68 int x = 0; 69 int y = 0; 70 int i = 1; 71 while(x != n-1 || y != m-1) 72 { 73 if(mp[x][y] >= '1' && mp[x][y] <= '9') 74 { 75 int stop = mp[x][y] - '0'; 76 while(stop--) 77 { 78 printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y); 79 } 80 } 81 printf("%ds:(%d,%d)->(%d,%d)\n", i++, x, y, last[x][y]/1000, last[x][y]%1000); 82 83 int t = last[x][y]/1000; 84 y = last[x][y]%1000; 85 x = t; 86 } 87 if(mp[x][y] >= '1' && mp[x][y] <= '9') 88 { 89 int stop = mp[x][y] - '0'; 90 while(stop--) 91 { 92 printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y); 93 } 94 } 95 } 96 97 int main() 98 { 99 //freopen("test.txt", "r", stdin); 100 while(~scanf("%d%d", &n, &m)) 101 { 102 memset(mp, 0, sizeof(mp)); 103 memset(v, 0, sizeof(v)); 104 memset(last, 0, sizeof(last)); 105 for(int i = 0; i < n; i++) 106 { 107 scanf("%s", mp[i]); 108 } 109 110 if(bfs()) output(); 111 else printf("God please help our poor hero.\n"); 112 printf("FINISH\n"); 113 } 114 return 0; 115 }
View Code
转载于:https://www.cnblogs.com/mypride/p/4695168.html
hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)相关推荐
- 【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)
题干: The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pr ...
- HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
这个问题是一个典型的类型的问题迷宫广泛的搜索. 在网上看到了很多解决方案. 没什么解决问题的分析报告,不指出其中的关键点.代码更像是一大抄.一些分析师也有很大的文章分析.只是不要全部命中关键,什么是广 ...
- HDU 1026 Ignatius and the Princess I(BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 在这博客学的 ttp://www.wutianqi.com/?p=2354 感觉看了这个之后收获 ...
- hdu 1398 Square Coins/hdu 1028 Ignatius and the Princess III
两道母函数的模板题: http://acm.hdu.edu.cn/showproblem.php?pid=1398 View Code #include<iostream>#include ...
- HDU - 1029 Ignatius and the Princess IV
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=1029 题目大意 给你n(n为奇数, n < 1e6)个数,让你找到个数 >=( ...
- Hdu 1029 Ignatius and the Princess IV
思路:普通的cin会超时 方法很妙 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 ...
- HDU 1028 Ignatius and the Princess III
//强行递推. xx[i][j]表示i数中第j个开头的组合种类. /* 最终结果[i]为 sum of(xx[i][j]) (j from 1 to i); xx[i][j]=sum of (xx ...
- hdu 1028 Ignatius and the Princess III 母函数入门
传送门 文章目录 题意: 思路: 题意: 给你一个数nnn,问你有多少种方案用1−n1-n1−n的数能组成nnn,数的使用次数无限制. n≤120n\le120n≤120 思路: 考虑构造母函数. 对 ...
- hdu1026 Ignatius and the Princess I (bfs)
题目意思就是说, hero 去城堡里救公主: 城堡的规模是n * m 的, 刚开始的时候hero是在(0,0)位置, 要求hero最终到达(n-1, m-1)的最少时间,并输出路径: 输入包含n行m列 ...
最新文章
- 狂神说Java 之SpringBoot整合Shiro框架笔记!
- nginx怎么部署php项目,nginx怎么正确部署前端项目
- oracle查询转insert语句,oracle中将查出来的数据转化为insert into语句
- Codeforces 500
- ubuntu deepin python/python3安装pip/pip3
- 【转】C++标准转换运算符static_cast
- 给linux用户加入sudo权限
- 口腔取模过程及注意事项_康贝齿科首家引进LAUNCA数字化口腔扫描,走进口腔数字化诊疗时代!...
- 静态资源详解和帮助文档的使用
- POI的XWPFTableCell的方法
- 公众号内打开提示404_200元500元/篇 | 她家小酒馆儿公众号征稿!(三天内审核、有额外稿费)...
- VC++可视化编程——创建空白窗口
- 计算机高级职称考试报考条件,高级工程师证怎么考 报考条件
- ansible-playbook 通过mail模块发送邮件
- 如何在html中做超链接,如何在HTML上做一个超链接?
- 为什么公务员需要考MPA?
- Flink源码剖析:回撤流
- Java web(简单的servlet计算器)网页计算器
- org.elasticsearch.common.util.concurrent.EsRejectedExecutionException 查询超时异常处理记录---一定要用单例模式
- 信息系统安全开发注意事项(一)
热门文章
- matlab正弦光栅,科学网—光栅-正弦,矩形 - 宗兆玉的博文
- uboot修改linux源码中参数,将自定义参数从uboot传入kernel的并读取的方法【转】
- 图片怎么等比缩放_mac图像缩放工具Teorex iResizer
- php 获取下拉框选中的文本,jQuery如何获取select选择的文本与值?(代码示例)...
- mysql中存储引擎是啥_mysql中的存储引擎
- python苹果下载软件助手哪个好_Mac上有什么实用的必备软件?
- html点击按钮弹出窗口_电脑桌面总是弹出广告怎么办?教你2种方法,轻松解决...
- 算法设计原则验证实验报告_算法设计与分析实验报告 统计数字问题
- 二十四、TextCNN的原理和实现
- 自然语言处理之循环神经网络(五)