hdoj 1026 Ignatius and the Princess I 最小步数,并且保存路径
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11167    Accepted Submission(s): 3411
Special Judge
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 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.
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.
.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.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
using namespace std;
char map[][];
int vis[][],n,m,mins;
bool rescue;
int dir[][]={{,},{-,},{,},{,-}};
struct node
{
int x,y,cnt;
node(int xx=,int yy=, int ccnt=) : x(xx),y(yy),cnt(ccnt){};//不知道什么意思
friend bool operator <(const node &a,const node &b)
{
return a.cnt>b.cnt;
}
}f[][];;
void bfs()
{
node t;
t.x=;t.y=;t.cnt=;
priority_queue< node >Q;
Q.push(t);
vis[][]=;
while(!Q.empty())
{
node b=Q.top();
Q.pop();
if(b.x==n && b.y==m)
{
rescue=; mins=b.cnt; return ;
}
for(int k=; k< ; k++)
{
int i = b.x+dir[k][];
int j = b.y+dir[k][];
if(i<=n && i> && j<=m && j> && !vis[i][j] && map[i][j]!='X')
{
//cout<<"***"<<endl;
vis[i][j]=;
f[i][j].x=b.x ; f[i][j].y=b.y; f[i][j].cnt=b.cnt+;//保存前一个位置
if(map[i][j]=='.')
Q.push(node(i,j,b.cnt+));
else Q.push(node(i,j,b.cnt+(map[i][j]-'')+));
}
}
}
}
void print()
{
stack < node > S;
node temp = f[n][m];
S.push(node(n,m,mins));
while(temp.x!= || temp.y!=)//提取出,所有的位置;
{
S.push(temp);
temp=f[temp.x][temp.y];
}
int t=;
while(!S.empty())
{
temp=S.top();
S.pop();
if(map[temp.x][temp.y]=='.')
printf("%ds:(%d,%d)->(%d,%d)\n",t++,f[temp.x][temp.y].x-,f[temp.x][temp.y].y-,temp.x-,temp.y-);
else {
printf("%ds:(%d,%d)->(%d,%d)\n",t++,f[temp.x][temp.y].x-,f[temp.x][temp.y].y-,temp.x-,temp.y-);
int k=map[temp.x][temp.y]-'';
while(k--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,temp.x-,temp.y-);
}
}
printf("FINISH\n");
return ;
}
int main()
{
while(cin>>n>>m)
{
for(int i=; i<=n; i++)
for(int j=;j<=m;j++)
cin>>map[i][j];
memset(vis,,sizeof(vis));
rescue=;
bfs();
// cout<<rescue<<endl;
if(rescue) printf("It takes %d seconds to reach the target position, let me show you the way.\n",mins);
else {printf("God please help our poor hero.\nFINISH\n");continue;}
print(); }
}
hdoj 1026 Ignatius and the Princess I 最小步数,并且保存路径的更多相关文章
- hdu 1026 Ignatius and the Princess I
		
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
 - 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 ...
 - 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 ...
 - 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 ...
 - hdu 1026   Ignatius and the Princess I    搜索,输出路径
		
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
 - HDOJ.1029 Ignatius and the Princess IV(map)
		
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
 - HDOJ 1028 Ignatius and the Princess III (母函数)
		
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
 - HDU 1026 Ignatius and the Princess I(BFS+优先队列)
		
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
 - hdoj 1027 Ignatius and the Princess II 【逆康托展开】
		
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
 
随机推荐
- 学会自己写jQuery插件(二)---自己写的tab插件
			
通过上一个基础篇我们知道插件的格式,这次我来写一个tab插件 $(function() { $.fn.插件名称 = function(options) { var defaults = { Event ...
 - 如何在Django1.6结合Python3.4版本中使用MySql
			
唉,最近赶了个新潮,用起了Python3.4跟Django1.6,数据库依然是互联网企业常见的MySql. 悲催的是在Python2.7时代连接MySql的MySQLdb还不支持Python3.4,还 ...
 - [Node.js] Level 6. Socket.io
			
6.2 Setting Up socket.io Server-Side So far we've created an Express server. Now we want to start bu ...
 - Jfinal极速开发微信系列教程(二)--------------让微信公众平台通过80端口访问本机
			
概述: 微信公众平台要成为开发者,需要填写接口配置信息中的“URL”和“Token”这两项(参见:http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E ...
 - (剑指Offer)面试题39:判断平衡二叉树
			
题目: 输入一课二叉树的根结点,判断该树是不是平衡二叉树.如果二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树. 思路: 1.重复遍历结点 参考上一题求二叉树的深度,先求出根结点 ...
 - nginx的 CPU參数worker_processes和worker_cpu_affinity使用说明
			
Nginx默认没有开启利用多核CPU,我们能够通过添加worker_cpu_affinity配置參数来充分利用多核CPU.CPU是任务处理,计算最关键的资源,CPU核越多.性能就越好. worker_ ...
 - cocos2d-x 音乐与音效
			
1.背景音乐 要使用一个音乐,首先要预加载这个音乐,预加载的方法如下 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCF ...
 - iOS网络监控— BMReachability
			
1. What's BMReachability? BMReachability是基于AFNetworking的Reachability类封装的监听网络状态变化的组件. 它在AF提供的无网络/wifi ...
 - 〖Linux〗ltib的使用帮助
			
scue@Link:/home/work/ltib$ ./ltib --help This script is used to manage the building of BSPs with com ...
 - LoadRunner 录制 mobile
			
方法一:本地安装安卓模拟器,用LR选择模拟器录制方式录制 方法二:手机真机需要root,可以在电脑上下载一键root工具(如卓大师),然后手机和电脑用数据线连接,然后root. 在手机上运行 Mobi ...