Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14677    Accepted Submission(s): 4653
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
 
Recommend
We have carefully selected several similar problems for you:  1072 1180 1372 1043 1254 
 
这道题的题目特别长,同样要输出走过的路径,采用了优先队列和以前写过的方法输出,测试数据过了,但提交超时,所以查找了网上大神的代码,优先队列并不会超时,而应该是我的输出方法有问题,于是使用了深搜的方法输出,感觉还是很方便的。
 
题意:骑士(地图左上角)要去救公主(地图右下角),而他们之间(地图上)有3种物体,‘.’为路,可以通过;‘X’为墙,不能通过;'1'~'9'数字为怪物的血量num。骑士遇到怪物,要停留num秒,求骑士能否到达公主的位置,如果能到达,输出最短路径的长度,并输出路径,如果不能到,输出特定语句。
 
附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct node
{
int x,y,step;
friend bool operator < (node n1,node n2) //优先队列,队列从小到大排序
{
return n1.step>n2.step;
}
} s1,s2,ss[][]; //ss数组记录走的所有路径
char map[][];
int visit[][],n,m,t,p;
int f[][]= {,,,-,,,-,}; void BFS()
{
priority_queue <node> q; //优先队列的定义
while(!q.empty())
q.pop();
s1.x=;
s1.y=;
s1.step=;
visit[][]=;
ss[s1.x][s1.y].x=; //起点为左上角
ss[s1.x][s1.y].y=;
q.push(s1);
while(!q.empty())
{
s1=q.top();
q.pop();
if(s1.x==n-&&s1.y==m-) //终点为右下角
{
p=s1.step;
return;
}
for(int i=; i<; i++)
{
s2=s1;
s2.x=s1.x+f[i][];
s2.y=s1.y+f[i][];
if(s2.x>=&&s2.x<n&&s2.y>=&&s2.y<m&&map[s2.x][s2.y]!='X'&&!visit[s2.x][s2.y])
{
visit[s2.x][s2.y]=;
s2.step=s1.step+;
if(map[s2.x][s2.y]>=''&&map[s2.x][s2.y]<='') //遇到怪物的时候需要耗得时间
s2.step=s2.step+map[s2.x][s2.y]-'';
ss[s2.x][s2.y].x=s1.x; //记录走到这一步的上一步坐标
ss[s2.x][s2.y].y=s1.y;
q.push(s2);
}
}
}
p=-; //到不了的标记
return;
} void print(int x,int y) //深搜输出,从终点开始往前搜
{
if(x==&&y==) return; //找到起点,返回上一次
print(ss[x][y].x,ss[x][y].y);
printf("%ds:(%d,%d)->(%d,%d)\n",t++,ss[x][y].x,ss[x][y].y,x,y);
if(map[x][y]>=''&&map[x][y]<='') //如果遇到怪兽,多呆几秒的输出
{
int w=map[x][y]-'';
for(int i=w; i>; i--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,x,y);
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<n; i++)
scanf("%s",&map[i]);
memset(visit,,sizeof(visit));
BFS();
t=;
if(p==-) //到不了的特定输出
printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",p); //输出一共需要多少秒
print(n-,m-);
}
printf("FINISH\n"); //别忘了需要输出的最后一句话
}
return ;
}
 

hdu 1026 Ignatius and the Princess I(bfs)的更多相关文章

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

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

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

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

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

  4. hdu 1028 Ignatius and the Princess III(母函数)

    题意: N=a[1]+a[2]+a[3]+...+a[m];  a[i]>0,1<=m<=N; 例如: 4 = 4;  4 = 3 + 1;  4 = 2 + 2;  4 = 2 + ...

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

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

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

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

  7. HDU 1026 Ignatius and the Princess I (广搜)

    题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...

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

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

  9. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. 第十章—DOM(二)——Element类型

    Element类型用于表现HTML和XML,提供了对元素标签名,子节点和特效的访问.Element节点具有以下特征: 要访问元素的标签名,可以使用nodeName属性,也可以使用tagName属性.这 ...

  2. Python 五个知识点搞定作用域

    Python 五个知识点搞定作用域 1.块级作用域 想想此时运行下面的程序会有输出吗?执行会成功吗? #块级作用域 if 1 == 1: name = "lzl" print(na ...

  3. 【JZOJ3296】【SDOI2013】刺客信条(assassin)

    ╰( ̄▽ ̄)╭ Description 故事发生在1486 年的意大利,Ezio 原本只是一个文艺复兴时期的贵族,后来因为家族成员受到圣殿骑士的杀害,决心成为一名刺客.最终,凭借着他的努力和出众的天赋 ...

  4. 将CMD命令提示符的起始位置进行更改 / CMD起始位置发生改变后如何修改回来

    具体步骤如下: 1.首先我们需要先找到命令提示符所在的文件目录.可以在开始运行程序中输入CMD,一般回自动搜索匹配. 2.右键点击命令提示符,在弹出菜单中,选择“打开文件位置”: 3.然后我们就可以进 ...

  5. tcpdump抓取udp报文

    使用tcpdump命令抓取UDP 2000端口报文,并将报文保存到当前目录下的udp.cap文件,命令如下: tcpdump -i 网络接口名称 udp port 2000 -w ./udp.cap ...

  6. JS判断PC 手机端显示不同的内容

    方法一: function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android ...

  7. python判断输入日期是该年的第几天

    1.输入日期,判断日期是该年度的第几天 iyear = int(input("请输入年:\n")) imonth = int(input("请输入月:\n")) ...

  8. iOS 9 学习系列:Split Screen Multitasking

    http://www.cocoachina.com/ios/20151010/13601.html iOS 9 的一个重大变化就是增加了多任务,这个多任务允许用户在屏幕上同时运行多个 app.有两种形 ...

  9. 在MaxCompute中配置Policy策略遇到结果不一致的问题

    背景信息: 本文以如下场景为基准进行编写,如下: 用户通过DataWorks-简单模式使用MaxCompute: 用户具有DataWorks默认角色,如DataWorks开发者角色: 用户通过cons ...

  10. CDH5.13.1安装

    文件下载 Cloudera Manager 地址:http://archive.cloudera.com/cm5/cm/5/ 这里下载的是5.13.1的版本,https://archive.cloud ...