Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10312    Accepted Submission(s): 3125 Special Judge

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
 
Author
Ignatius.L
 
 /*

 搜索,输出走的路径。
用一个falg数组保存每一次的方向,所以最后 递归的求出来就可以了。 优先队列的两种不同写法。
在搜索的过程中,不能出现走回头路的情况。这里使用了一个把后路堵死的方法。
“f[x1][y1]=-1;//防止重复访问。”
而且优先队列,对于一个点来说,第一次访问的time就是最小的。 */ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int n,m,time1,TT;
int f[][];
int cnt[][];
int flag[][];
int map1[][]={ {,},{,},{-,},{,-} };
struct node
{
int x,y;
int t;
};
struct cmp
{
bool operator() (const node &n1,const node &n2) //!!注意有的不是这样写的。
{
return n1.t>n2.t;
}
}; int bfs()
{
int i,x1,y1;
node next,cur;
priority_queue<node,vector<node>,cmp>Q;//!
cur.x=; cur.y=; cur.t=;
f[][]=-;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
for(i=;i<;i++)
{
x1=cur.x+map1[i][];
y1=cur.y+map1[i][];
next.x=x1;
next.y=y1;
if(x1>=&&x1<=n &&y1>=&&y1<=m && f[x1][y1]!=-)
{
next.t=cur.t+f[x1][y1]+;
Q.push(next);
f[x1][y1]=-;//防止重复访问。
flag[x1][y1]=i+;//记录 路径
if(x1==n && y1==m)
return next.t;
}
}
}
return ;
} void print(int x,int y)
{
int x1,y1;
if(flag[x][y]==)return;
x1=x-map1[flag[x][y]-][];
y1=y-map1[flag[x][y]-][]; print(x1,y1);
printf("%ds:(%d,%d)->(%d,%d)\n",TT++,x1-,y1-,x-,y-);
while(cnt[x][y])
{
printf("%ds:FIGHT AT (%d,%d)\n",TT++,x-,y-);
cnt[x][y]--;
}
return;
} int main()
{
int i,j,Sum;
char a[];
while(scanf("%d%d",&n,&m)>)
{
memset(cnt,,sizeof(cnt));
memset(flag,,sizeof(flag));
for(i=;i<=n;i++)
{
scanf("%s",a+);
for(j=;j<=m;j++)
{
if(a[j]=='X')
f[i][j]=-;
else if(a[j]=='.')
f[i][j]=;
else f[i][j]=cnt[i][j]=a[j]-'';
}
}
TT=;
Sum=bfs();
if(Sum)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",Sum);
print(n,m);
}
else
printf("God please help our poor hero.\n"); printf("FINISH\n");
}
return ;
}

hdu 1026 Ignatius and the Princess I 搜索,输出路径的更多相关文章

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

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

  4. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

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

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

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

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

  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)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. PHP一句话木马Webshell变形免杀总结

    0×00 前言 大部分Webshell查杀工具都是基于关键字特征的,通常他们会维护一个关键字列表,以此遍历指定扩展名的文件来进行扫描,所以可能最先想到的是各种字符串变形,下面总结了一些小的方法,各种不 ...

  2. 在macro中怎么接着使用一些库?(遇到的例子:current_user)

    这回是用的stackoverflow http://stackoverflow.com/questions/26339583/ 在调用模板html的时候 写上一个 with context 整体效果 ...

  3. [转] 红帽7搭建Zabbix监控

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决 ...

  4. Java中类变量和实例变量的初始化

    1. 类变量和实例变量 类变量即类成员变量中的静态变量,它们可以通过类名来直接访问. 实例变量是类成员变量中的非静态变量,只有在实例化对象之后通过对象来访问. 2. 空间分配的时间不同 类变量是在类加 ...

  5. Bootstrap Modal 关闭时右侧滚动条消失,页面左移的解决方法

    问题描述:页面在打开Modal之前右侧有滚动条,Modal关闭之后,body中的class="modal-open"和style="padding-right: 17px ...

  6. Java中常见的jar包及其主要用途

    jar包        用途 axis.jar     SOAP引擎包 commons-discovery-0.2.jar     用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周 ...

  7. P03-Python装饰器

    本文总结自oldboy python教学视频. 一.前言 1.装饰器本质上就是函数,功能是装饰其他函数,为其他函数添加附加功能. 装饰器在装饰函数时必须遵循3个重要的原则: (1)不能修改被装饰的函数 ...

  8. c/c++ int,float,short 大小端转换函数

    unsigned int(uint32_t)大小端转换函数 unsigned int BLEndianUint32(unsigned int value) { return ((value & ...

  9. PHP正则表达式笔记和实例

    转自:  https://www.cnblogs.com/yafei236/p/4168290.html 本文主要介绍如何在PHP使用正则表达式,并附带几个实例. 这两天工作用到了正则表达式,发现自己 ...

  10. 利用flask-sqlacodegen快速导入ORM表结构

    利用flask-sqlacodegen快速导入ORM表结构 友情提示:如果是使用pymysql请预先pip install 哦~ 这是window下使用virtualenv环境下执行的 Linux用户 ...