hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了。
题意比较明确,给一张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+记录路径)的更多相关文章
- 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(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)
题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...
- 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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 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(优先队列 + 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+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
随机推荐
- Synchronized Methods
Synchronized Methods The Java programming language provides two basic synchronization idioms: synchr ...
- 深入理解JVM—字节码执行引擎
原文地址:http://yhjhappy234.blog.163.com/blog/static/3163283220122204355694/ 前面我们不止一次的提到,Java是一种跨平台的语言,为 ...
- hdu1021 Fibonacci Again
http://acm.hdu.edu.cn/showproblem.php?pid=1021 //找规律.. #include<iostream> #include<stdio.h& ...
- 原版win7镜像IE主页被篡改?
装了几次系统,镜像是从“MSDN我告诉你”上下载的,但是每次装完,发现IE主页不是microsoft的官方网页,而是www.3456.com. 这就奇了怪了,难道“MSDN我告诉你”提供的不是原版镜像 ...
- XML DOS 攻击
内容来自此文:http://msdn.microsoft.com/en-us/magazine/ee335713.aspx DoS(Denial of service:拒绝服务)攻击由来已久(1992 ...
- 【Linux高频命令专题(19)】vi/vim
概述 其实在Linux中一切命令或者软件都是文件,所以把vi/vim作为高频命令专题之一,也没什么不妥.虽然大家都称之为编辑器~~ vim是vi的高级版本,比如有代码高亮,也就是说可以把vim定位为程 ...
- WCF入门(四)---WCF架构
WCF是一个分层架构,为开发各种分布式应用的充分支持.该体系结构在下面将详细说明. 约定 约定层旁边就是应用层,并含有类似于现实世界的约定,指定服务和什么样的信息可以访问它会使操作的信息.约定基本都是 ...
- asp.net(C#)读取word 文档的方法
第一种方法 Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "Application/msw ...
- svn:...target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)
在上传项目到svn时,pom.xml报错
- MyEclipse 2014GA 新建 Web Project 并配置 SSH
基本软件配置: 1)MyEclipse 2014GA(JDK:内置 1.7.0.u45:SSH:内置 Struts2.1.Spring3.1 和 Hibernate4.1) 2)apache- ...