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. 官宣,PyTorch 1.0 稳定版本现已推出

    简评:快来一起快乐地学习吧. 随着 PyTorch 生态系统和社区继续为开发人员提供有趣的新项目和教育资源,今天(12 月 7日)在 NeurIPS 会议上发布了 PyTorch 1.0 稳定版.研究 ...

  2. 老男孩Day12作业:RabbitMQ-RPC版主机管理程序

    一.作业需求 1.可以对指定机器异步的执行多个命令 例子: 请输入操作指令>>>:run ipconfig --host 127.0.0.0 in the call     tack ...

  3. java学习笔记_接口

    接口:interface(关键字) public interface USB {} 1. 接口中都是抽象方法,方法前面的可见度(public.private)和抽象关键字(abstract)可以不写. ...

  4. sourceTree"重置提交"和"提交回滚"的区别

    相信用过sourceTree的伙伴们都认识这两,但是不一定用过这两个功能,甚至是不能很好的把握它两的区别,根据自己最近亲身测试,总算是能小小的总结一下了 首先这儿假如,历史版本已经出现了1.2.3.4 ...

  5. 2016级算法第六次上机-D.AlvinZH的学霸养成记V

    1081 AlvinZH的学霸养成记V 思路 中等题,计算几何. 这是一个排序问题,按极角排序.可以转化为叉积的应用,对于点A和B,通过叉积可以判断角度大小,共线时再判断距离. 叉积的应用.OA × ...

  6. C#-进制转化

    (一)数制 计算机中采用的是二进制,因为二进制具有运算简单,易实现且可靠,为逻辑设计提供了有利的途径.节省设备等优点,为了便于描述,又常用八.十六进制作为二进制的缩写.一般计数都采用进位计数,其特点是 ...

  7. 微信小程序之蓝牙 BLE 踩坑记录

    前言 前段时间接手了一个微信小程序的开发,主要使用了小程序在今年 3 月开放的蓝牙 API ,此过程踩坑无数,特此记录一下跳坑过程.顺便开了另一个相关的小项目,欢迎 start 和 fork: BLE ...

  8. 一种很有意思的数据结构:Bitmap

    昨晚遇到了一种很有意思的数据结构,Bitmap. Bitmap,准确来说是基于位的映射.其中每个元素均为布尔型(0 or 1),初始均为 false(0).位图可以动态地表示由一组无符号整数构成的集合 ...

  9. React之表单

    第一部分:表单基础 在React中,修改表单的唯一途径是使用setState方法.举例如下: class NameForm extends React.Component { constructor( ...

  10. 【Sonarqube】windows下更改Temp文件夹的位置

    下载的最新Sonarqube版本(4.5.1),通过StartSonar.bat文件可以启动,但是无法通过StartNTService.bat文件启动,原因为默认的Temp文件不可写入, java.l ...