以前写的题了,现在想整理一下,就挂出来了。

题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1)。

'X'为墙,'.'为路,数字为怪物。墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间。

比较麻烦的是需要记录路径。还要记录是在走路还是在打怪。

因为求最短路,所以可以使用bfs。

因为进过每一个点花费时间不同,所以可以使用优先队列。

因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点。当然,记录方法不止一种。

上代码——

 #include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std; struct node
{
int x, y, step;
bool operator < (const node& a) const
{
return a.step < step;
}
}; int go[][] = {{, },{-, }, {, }, {, -}}; int n, m, step;
bool v[][];
char mp[][];
int last[][]; bool bfs()
{
node p, q;
p.x = n-;
p.y = m-;
p.step = ;
if(mp[n-][m-] >= '' && mp[n-][m-] <= '') p.step += mp[n-][m-] -''; priority_queue <node> que;
if(mp[p.x][p.y] != 'X')
que.push(p);
v[p.x][p.y] = ; while(!que.empty())
{
p = que.top();
que.pop(); for(int i = ; i < ; i++)
{
int x = p.x+go[i][];
int y = p.y+go[i][]; if(x >= && x < n && y >= && y < m && !v[x][y])
{
if(mp[x][y] == 'X') continue; q.x = x; q.y = y; q.step = p.step+;
if(mp[x][y] >= '' && mp[x][y] <= '') q.step += mp[x][y]-'';
que.push(q);
v[x][y] = ;
last[x][y] = p.x*+p.y;
if(x == && y == ) {step = q.step; return ;} }
}
}
return ;
} void output()
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", step);
int x = ;
int y = ;
int i = ;
while(x != n- || y != m-)
{
if(mp[x][y] >= '' && mp[x][y] <= '')
{
int stop = mp[x][y] - '';
while(stop--)
{
printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
}
}
printf("%ds:(%d,%d)->(%d,%d)\n", i++, x, y, last[x][y]/, last[x][y]%); int t = last[x][y]/;
y = last[x][y]%;
x = t;
}
if(mp[x][y] >= '' && mp[x][y] <= '')
{
int stop = mp[x][y] - '';
while(stop--)
{
printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
}
}
} int main()
{
//freopen("test.txt", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
memset(mp, , sizeof(mp));
memset(v, , sizeof(v));
memset(last, , sizeof(last));
for(int i = ; i < n; i++)
{
scanf("%s", mp[i]);
} if(bfs()) output();
else printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return ;
}

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)的更多相关文章

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

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

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

  3. HDU 1026 Ignatius and the Princess I (BFS)

    题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...

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

  5. hdu 1026 Ignatius and the Princess I

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

  6. HDU1026--Ignatius and the Princess I(BFS记录路径)

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

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

  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+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

随机推荐

  1. nginx模块开发(18)—日志分析

    1.日志简介 nginx日志主要有两种:访问日志和错误日志.访问日志主要记录客户端访问nginx的每一个请求,格式可以自定义:错误日志主要记录客户端访问nginx出错时的日志,格式不支持自定义.两种日 ...

  2. HDU 1452 Happy 2004(因子和的积性函数)

    题目链接 题意 : 给你一个X,让你求出2004的X次方的所有因子之和,然后对29取余. 思路 : 原来这就是积性函数,点这里这里这里,这里讲得很详细. 在非数论的领域,积性函数指所有对于任何a,b都 ...

  3. hdu 4465 Candy 数学

    思路:易知结果为 ∑(n-k)*C(n+k,k)*(p^(n+1)*q^k+q^(n+1)*p^k). 注意不能直接算,注意点技巧!!!看代码 代码如下: #include<iostream&g ...

  4. 使用 C# 对文件进行压缩和解压

    C#中对文件压缩和可以使用两个类: GZipStream 类 此实例分为几个模块,分别为: 压缩函数: /// <summary> /// 压缩文件 /// </summary> ...

  5. redis、memcache、mongoDB有哪些区别(转载)

    转载: http://leandre.cn/database/64.html Memcached Memcached的优点: Memcached可以利用多核优势,单实例吞吐量极高,可以达到几十万QPS ...

  6. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  7. Java并发:Callable、Future和FutureTask

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  8. login.java

    import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Login extends JFrame ...

  9. BCB一个问过100遍啊100遍的问题

    一个问过100遍啊100遍的问题作者: ---------- ,如转载请保证本文档的完整性,并注明出处.欢迎光临 C++ Builder 研究, http://www.ccrun.com/doc/go ...

  10. 本人arcgis api for javascript中常见错误总结

    1. 2.对象不支持"replace"属性或方法 解决办法:一般在ie中执行js会报这样的错误,基本问题就是你引用了某个对象中不存在的方法,可能是这个方法本来存在而你写错了,或者调 ...