HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026
题意:给出一个迷宫,求出到终点的最短时间路径。
这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这个时候可以用优先队列,每次让时间最短的出队列。由于最后还需要输出路径,所以需要设置一个数组来保存路径。
#include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int maxn = + ; struct node
{
int x, y;
int time;
friend bool operator < ( node a, node b) //重载<号
{
return b.time<a.time;
}
}; char map[maxn][maxn];
int visited[maxn][maxn];
int path[maxn][maxn];
int n, m;
int d[][] = { { , }, { -, }, { , }, { , - } }; int bfs()
{
node q,now;
priority_queue<node> p;
q.x = ;
q.y = ;
q.time = ;
p.push(q);
while (!p.empty())
{
q = p.top();
p.pop();
if (q.x == n - && q.y == m - ) return q.time;
for (int i = ; i < ; i++)
{
int xx = q.x + d[i][];
int yy = q.y + d[i][];
now.x = xx;
now.y = yy;
now.time = q.time;
if (xx >= && xx < n && yy >= && yy < m && map[xx][yy]!='X' && !visited[xx][yy])
{
if (map[xx][yy] == '.') now.time++;
else now.time =now.time+ (map[xx][yy] - ''+);
visited[xx][yy] = ;
path[xx][yy] = i + ; //记录路径
p.push(now);
}
}
}
return -;
} int temp; void print(int x, int y)
{
int xx, yy;
if (path[x][y] == ) return;
xx = x - d[path[x][y] - ][]; //寻找第一个路径点
yy = y - d[path[x][y] - ][];
print(xx, yy);
printf("%ds:(%d,%d)->(%d,%d)\n", temp++, xx, yy, x, y);
if (map[x][y] <= '' && map[x][y] >= '')
{
int m = map[x][y] - '';
while (m--) printf("%ds:FIGHT AT (%d,%d)\n", temp++, x, y);
}
} int main()
{
while (cin >> n >> m && (n||m))
{
memset(visited, , sizeof(visited));
memset(path, , sizeof(path));
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
cin >> map[i][j];
visited[][] = ;
int ans=bfs();
if (ans == -) cout << "God please help our poor hero." << endl;
else
{
cout << "It takes " << ans << " seconds to reach the target position, let me show you the way." << endl;
temp = ;
print(n - ,m - );
}
cout << "FINISH" << endl;
}
return ;
}
HDU 1026 Ignatius and the Princess I(带路径的BFS)的更多相关文章
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I(bfs)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- apache中虚拟主机的配置
一.两种方式:基于域名的虚拟主机和基于IP地址的的虚拟主机 (这里基于前者) 二.作用:实现在同一个web服务器下,同时运行很多个站点(项目) 三.虚拟主机的配置 1.在核心配置文件中加载虚拟主机配置 ...
- HDU 1222
题意: 一头狼和一头兔子在一座山中,给你一个数n表示洞的个数,编号从0~n-1.兔子可以随意躲在其中一个洞中,狼每次都从编号为0的洞出发,接下来走到第m个洞中,问兔子能不能活下来,即不被狼吃掉.例如: ...
- Win环境下的文件读写
在win环境下,有许多方法可以对文件进行读写操作,如MFC 中的CFile类,及一些开源的项目如QT中的QFile.开源的好得是可以多平台,而MFC只是微软自家的东西,对于想写跨平台的人,最好不用MF ...
- Linux快捷键和别名
一.设置别名 1使用命令行 alias 别名='命令'(只对本次登陆生效) 2.使用配置文件设置别名(永久生效) vi /root/.bashrc 打开系统别名配置文件,一般是用 ...
- Dom学习笔记
今天老师出了一道面试题目:取到表单里面的textbox的值,两种方法.知道一种,老师说的什么dom,我竟然不知道. 以前学html的时候,老师也重来没有提到dom的概念.javaScript只是学了一 ...
- HelloWorld Makefile Template
DEPDIR = build_dep TARGET_NAME = helloworld CFLAGS = -Wall SRCS = main.c SRCS += foo.c OBJS = $(SRCS ...
- iOS开发学习概述及知识整理
设计师设计出来了一个不错的引导界面,然而当我看到设计稿的时候,我们的app也没几天就要上线了.这个界面模仿了Evernote iOS app的风格. 我以迅雷不及掩耳盗铃之势开始在Xcode上编程,用 ...
- Activity 生命周期
Activity 的四种基本状态 1.运行态(Running) Activity 处于屏幕最前端,用户可见且获得焦点. 2.暂停态(Paused) Activity被置于后台,用户可见,但失去焦点 3 ...
- 想学好web前端,需要看哪些书籍
目前市场上HTML.CSS 类别书籍,都是大同小异,在当当网.卓越网搜索一下很多推荐.今天web前端大牛根据自己的经验总结如下:Javascript 的书籍推荐看老外写的,国内很多 Javascrip ...
- ASP.NET 中HTML和Form辅助方法
Form辅助方法 Form最重要的属性就是action和method,action指明form中的数据被提交到哪里,method指明用什么方法,默认为GET,下面是一个简单的例子: <form ...