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)的更多相关文章

  1. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. HDU 1026 Ignatius and the Princess I (广搜)

    题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...

  6. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. List<T>的对比

    对于类型的对比,linq原来的对比是区分不了的. 对两个list进行查询交集方法.交集,并集的函数是直接用Linq的,这里不再写. List<T> intersectList = quer ...

  2. Javascript常用对象的属性和方法

    javascript为我们提供了一些非常有用的常用内部对象和方法.用户不需要用脚本来实现这些功能.这正是基于对象编程的真正目的. 在javascript提供了string(字符串).math(数值计算 ...

  3. PostSharp-4.3.22安装包_KeyGen发布

    PostSharp-4.3.22安装包_KeyGen发布 请低调使用. 下载相关 PostSharp-4.3.22安装包_KeyGen.part1.rar PostSharp-4.3.22安装包_Ke ...

  4. fc23升级fc24及字体问题解决

    1,升级fc23到最新 dnf upgrade --refresh 2,安装dnf系统升级插件 dnf install dnf-plugin-system-upgrade 3,下载fc24包,忽略无法 ...

  5. AppCode 2016.2.3 发布,支持 Swift3 的特性

    AppCode 2016.2.3 (build 162.2380.5)发布了,AppCode 是一个全新的 Objective-C.Swift 的集成开发环境,用于帮助开发 Mac.iPhone 和 ...

  6. C# OOP 重要部分全解

    如果你有耐心,那就请你慢慢的往下看,肯定有你用的到的地方,请你相信我! 现在你看到的只是其中一部分后面,还有,还没更新出来,待续.... 类对象的定义 类是现实世界或思维世界中的实体在计算机中的反映, ...

  7. Android 数据库管理— — —删除数据

    package com.example.datebasetest; import android.content.ContentValues;import android.database.sqlit ...

  8. IIS7+windows 64位配置注意事项

    问题和解决办法 1  如果网站为Asp:再asp中注意启用父路径 2  操作必须使用一个可更新的查询:给用户iis_iusrs 一个完全控制的权限 3  Windows(64位IIS)未在本地计算机上 ...

  9. [Python Fabric] [SSH] Mac OS X 10.9 + Vagrant虚拟环境使用Python Fabric进行SSH远程登录的简单实验

    1. ssh客户端生成key $ Generating public/private rsa key pair. Enter file in which to save the key (/Users ...

  10. C++ Daily 《6》---- 类静态对象与函数静态对象

    C++ 的一个哲学基础是,你不应该为你使用的东西付出代价. class 拥有一个 static 成员,即使从未被用到,它也会被构造和析构: 而 函数拥有一个 static 成员, 如果这个函数从未被调 ...