Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

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.

 
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

 
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

 
Sample Input
5 6
.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.
 
Sample Output
It takes 13 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)
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)的更多相关文章

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

  2. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

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

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

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

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

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

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

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

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

随机推荐

  1. java动态绑定的情况分析

    java是面向对象的语言,java中多态的一种情况是动态绑定.所谓的动态绑定,分两种情况:当调用类方法的时候,java虚拟机会基于对象的引用类型来选择执行方法.当java调用一个实例方法的时候,他会根 ...

  2. javascript 学习笔记之JQuery中的Deferred对象

    Deffered是Jquery中的一个非常重要的对象,从1.5版本之后,Jquery中的ajax操作都基于Deffered进行了重构,这个对象的处理模式就像其他Javascript框中的Promise ...

  3. Ubuntu 下 安装QQ 截图工具

    1.由于ubuntu下是没有dll动态链接库的,所以需要安装一个软件wine,有这个东西之后,以后在ubuntu下就可以运行exe文件了.(wine是一款优秀的Linux系统平台下的模拟器软件,用来将 ...

  4. Ubuntu14.04不支持U盘exfat格式该如何解决

    转: http://www.jb51.net/os/Ubuntu/275158.html exfat是U盘的文件系统,很多系统都支持exfat格式的使用,但Ubuntu系统并不支持exfat格式,要如 ...

  5. 基于python做的抓图程序1.0.00版本

    #coding=gbkimport urllibimport urllib2import reimport osimport time# import readline def getHtml(url ...

  6. 如何在win7上面安装python的包

    最近在win7上面搞python,然后写的一些代码涉及到了对Excel的读写.所以需要用到包xlrd xlwt  xlutils. 但问题是这些包import后显示的是找不到.错误提示是:Import ...

  7. 【C语言】中的版本规范(C89 C99等)

    C语言中的版本 一.相关基础知识 ISO:国际标准化组织(International Organization for Standardization,ISO)简称ISO,是一个全球性的非政府组织,是 ...

  8. 如何设置路由器实现静态IP配置

    一.概述 嵌入式开发者,经常面对这样的环境:PC(windows)+虚拟机(linux)+开发板.我们希望三者都能相互通信,而且可以联网. 对于实验室只提供一根网线,而自己没有额外的增加端口数量的设备 ...

  9. MVC5 学习笔记1

    新装了vs2013 开始试着学习MVC5 首先用了2013的内置的框架 这里提三点 1. bootstrap (现已加入mvc5豪华套餐) 他的框架已经加入了bootstrap 3.0的版本(http ...

  10. 转:靠谱的代码和DRY

    http://www.cppblog.com/vczh/archive/2014/07/15/207658.html 靠谱的代码和DRY 上次有人来要求我写一篇文章谈谈什么代码才是好代码,是谁我已经忘 ...