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 ;
}
 

最新文章

  1. java 装饰者模式与继承的区别
  2. Java中系统属性Properties介绍 System.getProperty()参数大全
  3. canvas滤镜之简单的取反
  4. PHP输出表格的方法
  5. Hadoop概念学习系列之分布式数据集的容错性(二十七)
  6. Android四大组件之一:Activity
  7. Tomcat部署问题,Tomcat集群部署问题。
  8. IIS7 性能(内存、CPU、当前请求耗时)监测
  9. Android开发手记(32) 使用摄像头拍照
  10. 使用commons-fileupload进行上传
  11. JQuery实战——页面进度条效果
  12. vim编辑器的使用技巧
  13. Effective Java 之-----返回零长度的数组或集合而不是null
  14. 洛谷 [P2024] 食物链
  15. Django学习笔记(http协议与django安装)
  16. vue引入JQ的方法
  17. c++ cin cin.getline() getline()用法
  18. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞
  19. MySQL Execution Plan--EXPLAIN用法
  20. SSM文件上传

热门文章

  1. AppScan操作手册
  2. Python中输入和输出(打印)数据
  3. python学习笔记08-- socket编程
  4. 2018-9-30-C#-winforms-输入颜色转换颜色名
  5. node.js对象数据类型
  6. 【转载】【python】python练手项目
  7. idea 项目热部署设置
  8. github中markdown语言的使用规则
  9. Servlet接口
  10. (六)IO流之过滤流