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 ( ...
随机推荐
- tyvj P1403 关押罪犯 题解
P1403 [NOIP2010]关押罪犯 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他 ...
- 2016年终总结--一个Python程序猿的跨界之旅
时间过得真快.感觉15年年终总结刚写完,16年就结束了.看了blog,16年就写了可怜的8篇,对我来说16年还算顺风顺水. 真正可能出乎意料的是年底我离开了呆了2年半的龙图游戏,临时放弃了用了3年半的 ...
- iOS:第三方数据库文件FMDB的使用
第三方数据库FMDB •FMDB的使用:在sqlite的基础上,将sqlite中的函数进行封装产生的一个数据库文件. –FMDB的好处是对基本C库的封装,方便使用.同时还提供了多线程操作数据库带来的读 ...
- C/C++调用java---JNI常用函数
DefineClass jclass DefineClass(JNIEnv *env, jobject loader, const jbyte *buf, jsize bufLen ...
- fonts.conf 中文手册
FONTS-CONF(5) FONTS-CONF(5) 名称 fonts.conf -- 字体配置文件 文件概要 /etc/fonts/fonts.conf /etc/fonts/fonts.dtd ...
- vue - 减少打包后的体积
打包命令: npm:npm run build yarn:yarn run build 路径:/config/index.js 是否产生map文件,置为false.
- 格式化文本数据抽取工具awk
在管理和维护Linux系统过程中,有时可能需要从一个具有一定格式的文本(格式化文本)中抽取数据,这时可以使用awk编辑器来完成这项任务.发明这个工具的作者是Aho.Weinberg和Kernighan ...
- QtGui.QPen
The QtGui.QPen is an elementary graphics object. It is used to draw lines, curves and outlines of re ...
- MySQL 5.1参数
MySQL 5.1.73参数 Variable_name Valueauto_increment_increment 1auto_increment_offset 1autocommit ONa ...
- taro 填坑之路(二)taro 通过事件监听 实现组件间传值
1.组件传值的方式 2.事件监听原理 3.事件管理器 utils/event.js /** * 事件池(事件管理器) * 通过事件监听传值 */ class Event { constructor() ...