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

题意比较明确,给一张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. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  2. Java 理论和实践: 了解泛型

    转载自 : http://www.ibm.com/developerworks/cn/java/j-jtp01255.html 表面上看起来,无论语法还是应用的环境(比如容器类),泛型类型(或者泛型) ...

  3. zoj 3232 It's not Floyd Algorithm(强联通分量,缩点)

    题目 /******************************************************************/ 以下题解来自互联网:Juny的博客 思路核心:给你的闭包 ...

  4. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  5. SDUT1574组合数的计算(组合数)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1574 这个题,比较奇怪,是用递推去做的,我试了 ...

  6. Android ActionBar 关于tab的应用 以及 TabListener的方法详解

    actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...

  7. [转]Openstack Havana Dashboard测试和使用

    转贴一篇陈沙克老师的文章:http://www.chenshake.com/openstack-havana-dashboard-to-test-and-use/ Openstack Havana D ...

  8. Swift入门(十一)——类型转换与is、as操作

    三种操作:is.as?和as! Swift是强类型语言,但也允许开发者通过is.as?和as!这三种操作来对类型进行判断和强制转换.其中is用作类型判断,而as?和as!则分别是类型转换的可选形式和强 ...

  9. 常用的Linux终端

    常用的Linux终端 gnome-terminal (Gnome标配) xfce4-terminal (XFCE4标配) lxterminal (LXDE标配) konsole (KDE标配) 前面3 ...

  10. Centos硬件信息查看命令

    [root@yan-001 ~] # uname -a # 查看内核/操作系统/CPU信息的linux系统信息命令 [root@yan-001 ~] # head -n 1 /etc/issue # ...