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. Display all 2232 possibilities? (y or n)

    Linux下我在没输入任何命令的情况下摁了两下tab键,然后就出现了这个提示:Display all 2232 possibilities? (y or n) 我觉得摁y的话就会显示所有的现阶段命令. ...

  2. 零基础学习Python数据分析

    网上虽然有很多Python学习的教程,但是大多是围绕Python网页开发等展开.数据分析所需要的Python技能和网页开发等差别非常大,本人就是浪费了很多时间来看这些博客.书籍.所以就有了本文,希望能 ...

  3. Common xaml controls(补交作业)

    Common xaml controls 常见的xaml控件: 先上一段代码,把他们基本都实现出来: <Grid Name="MyGrid"> <Button N ...

  4. 在eclipse中,用maven创建web项目

    备注:该文档是之前学习时,根据网上其他童鞋的经验自己测试后梳理,如有侵权,请勿怪,感谢! 1.在eclipse中用maven创建项目,右键new>>Maven Project 2.点击ne ...

  5. IDEA自动生成TestNG的testng.xml的插件

    某地方见到的,自己实际操作一遍,记录一下.方便以后查询. 下载Create TestNG XML 插件,重启IDEA即可. 重启idea,新建Maven项目. pom.xml增加依赖 <depe ...

  6. 博客主题皮肤探索-添加新功能和fiddler的css/js替换

    还有前言 使用了主题之后,发现还差了一点功能.最新评论没有了,导致读者回复需要一点时间去找到底回复了哪条博客.于是就有了添加功能的想法. 如何调试CSS/JS 打开f12,可以看见加载的js资源都是混 ...

  7. python socket文件传输实现

    简单版 server(服务端) import socket import subprocess import struct import json import os share_dir = r'E: ...

  8. CentOS 7 主机名bogon解决办法

    转https://blog.csdn.net/qq_24221531/article/details/80334942 一.修改linux主机的配置文件/etc/hostname 和 /etc/hos ...

  9. Mac 10.12安装粘贴板增加工具ClipMenu

    说明:这个工具可以保留复制过的记录,并且可以快速调出之前复制过的内容,最开发时比较常用,支持图片等. 下载: (链接: https://pan.baidu.com/s/1qXJbM2o 密码: wef ...

  10. 开发者必备的12个JavaScript库

    现在 web 设计是最有趣的了,做好 web 设计不仅要熟练使用 Javascript,css 和 html 等,还要有自己的创意设计.为了方便大家发挥自己的创意,就产生了很多 JS 框架,Node. ...