hdu1026 Ignatius and the Princess I (优先队列 BFS)
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
#include<stdio.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef struct N
{
int i,j,time;
char c;
int fron,nam;
friend bool operator<(N n1,N n2)
{
return n2.time<n1.time;
}
}node;
node map[105][105];
stack<node> S;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},h,w,maxT,flog;
void InStack(node Map)
{
node q=Map;
while(q.fron!=-1)
{
S.push(q);
q=map[q.fron/w][q.fron%w]; }
S.push(q);
//printf("%d\n",p);
}
void BFS(void)
{
priority_queue<node> Q;
node q,p;
int e,x,y,k,i;
map[0][0].c='X'; map[0][0].fron=-1;
q.i=0;q.j=0;q.time=0;
Q.push(q);
while(!Q.empty())
{
q=Q.top();
Q.pop();
k=0;
for(e=0;e<4;e++)
{
y=q.i+dir[e][0];
x=q.j+dir[e][1];
if(y>=0&&y<h&&x>=0&&x<w&&map[y][x].c!='X')
{
map[y][x].fron=map[q.i][q.j].nam; if(map[y][x].c=='.')
map[y][x].time=q.time+1;
else
map[y][x].time=q.time+map[y][x].c-'0'+1;
map[y][x].c='X';
p.i=y; p.j=x;p.time=map[y][x].time;
if(p.i==h-1&&p.j==w-1)
{
maxT=map[h-1][w-1].time;
InStack(map[h-1][w-1]);
flog=1;
//printf("%d\n",maxT);
return ;
} Q.push(p);
}
}
}
}
int main()
{
int i,j,t;
node q,p;
while(scanf("%d%d",&h,&w)>0)
{
t=0;
for(i=0;i<h;i++)
{
getchar();
for(j=0;j<w;j++)
{
scanf("%c",&map[i][j].c);
map[i][j].i=i; map[i][j].j=j;
map[i][j].nam=t++;
}
}
if(map[h-1][w-1].c=='X')
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue ;
}
flog=0;
BFS();
if(!flog)
printf("God please help our poor hero.\n");
else
{
p=S.top();
S.pop();//printf("%d\n",map[1][4].time);
printf("It takes %d seconds to reach the target position, let me show you the way.\n",maxT);
for(i=1;i<=maxT;i++)
{
if(p.time<i)
{
q=S.top();
S.pop();
}
if(p.time<i)
printf("%ds:(%d,%d)->(%d,%d)\n",i,p.i,p.j,q.i,q.j);
else
printf("%ds:FIGHT AT (%d,%d)\n",i,p.i,p.j);
p=q;
}
}
printf("FINISH\n");
}
}
hdu1026 Ignatius and the Princess I (优先队列 BFS)的更多相关文章
- HDU1026 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) 带路径的广搜
此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Me ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- hdu1026.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+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu---------(1026)Ignatius and the Princess I(bfs+dfs)
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)
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
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> ...
随机推荐
- 转:Windows 8上强制Visual Studio以管理员身份运行
Windows 8的一个既安全又蛋疼之处是UAC的行为被改变了.以往在Windows 7中,只要关闭了UAC,自己的帐号又是本机管理员组的,任何程序都会以管理员身份启动.然而,在Windows 8上, ...
- JavaScript DOM编程艺术第二版学习(1/4)
接下来项目需要网页相关知识,故在大牛的指引下前来阅读本书. 记录方式:本书分四部分阅读,完成阅读之后会多写一篇包括思维导图的算是阅读指南的东西,浏览的童鞋看着指南可以跳过一些不必要的坑~ 当前水平:H ...
- tomcat上servlet程序的配置与处理servlet请求过程
手动配置: tomcat服务器下web项目的基本目录结构 |-tomcat根目录 |-webapps |-WebRoot : web应用的根目录 |-静态资源(html+css+js+image+ve ...
- window FILES——windows文件管理相关实例
C语言下有一套文件管理方案.C++语言下也有一套自己的文件管理方案.windows系统当然也有自己的一套文件管理方案啦.对于普通char类型为基础的字符使用哪种方案的解决办法都是一样的,但是对于宽字符 ...
- Fatal error: Uncaught SoapFault exception
Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in login\login.php ...
- MFC error C2065: “IDD_DIALOG1” : 未声明的标识符 转载
error C2065: “IDD_DIALOG1” : 未声明的标识符 1.编译时提示error C2065: “IDD_DIALOG1” : 未声明的标识符 2.错误的可能原因及解决方法如下: 原 ...
- 解决GDB输出Qt内置类型的显示问题
自从GDB 7.0之后,就加入了Pretty-Printer的这个概念.简单理解就是他可以让你用Python写一串脚本,然后让gdb去读取这串脚本后,可以自由的输出由你想自己定义的格式.我们直接举个简 ...
- Lsp修复
打开电脑,进入命令提示符窗口,快捷键win+r. 在窗口中输入“cmd”进入命令符窗口. 在窗口中输入:输入netsh winsock reset,然后按下回车键. 然后稍等片刻, ...
- 禁用微信 webview 调整字体大小
原文:http://www.grycheng.com/?p=2411 微信 webview 内置了调整字体大小的功能,对于网页的可用性来说是一个很实用的功能.一些网页的字体设置过小导致用户看不清文字, ...
- 打造轻量级自动化测试框架WebZ
一.什么是WebZ WebZ是我用Python写的“关键字驱动”的自动化测试框架,基于WebDriver. 设计该框架的初衷是:用自动化测试让测试人员从一些简单却重复的测试中解放出来.之所以用“关键字 ...