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

题意比较明确,给一张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. 创建和编辑 crontab 文件

    http://docs.oracle.com/cd/E24847_01/html/819-6951/sysrescron-24589.html 创建和编辑 crontab 文件 创建 crontab  ...

  2. 2013 ACM-ICPC长沙赛区全国邀请赛——Bottles Arrangement

    这题当时竟然没看啊…… 找规律:求和m+m+m-1+m-1+……前n项 ;}

  3. lintcode:最大子数组II

    题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...

  4. POSIX semaphore: sem_open, sem_close, sem_post, sem_wait

    http://www.cnblogs.com/BloodAndBone/archive/2011/01/18/1938552.html 一.Posix有名信号灯 1.posix有名信号灯函数 函数se ...

  5. Photoshop技巧:图层蒙版同步隐藏图层样式

    原效果: 添加图层蒙版后,遮住一半,图层样式仍在,如: 进入图层样式,勾选“图层蒙版隐藏效果” 最终效果:

  6. C# WinForm窗体 控件Control 的 Invalidate、Update、Refresh的区别

    Control.Refresh - does an Control.Invalidate followed by Control.Update.Refresh: 强制控件使其工作区无效并立即重绘自己和 ...

  7. 查看局域网内某个ip的mac地址

      首先需要ping一下对方的ip,确保本地的arp表中缓存对方的ip和mac的关系 C:\Windows\System32>ping 192.168.1.231 正在 Ping 192.168 ...

  8. C#6.0 VS2015

    https://msdn.microsoft.com/en-us/library/hh156499(v=vs.140).aspx This page lists key feature names f ...

  9. [51NOD1105]第k大的数(二分答案)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 先排序,二分上下界分别是最小的两个数和最大的两个数的乘积 ...

  10. 【转】android UI设计的一些心得与问题解决(无效果图)

    1.把Button或者ImageButton的背景设为透明或者半透明: 半透明<Buttonandroid:background="#e0000000" ... /> ...