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 ( ...
随机推荐
- 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package
In the last years a lot of data has been released publicly in different formats, but sometimes the d ...
- mac下python2.x和python3.x的安装方法和升级方法/卸载
一.首先问个问题,我们为什么要升级python2.x或者python3.x的版本? 一个是低版本会有些bug:或者功能问题,或者安全问题等,另外高版本会引进一些新的功能,也会废弃一些老的功能. 可以通 ...
- Linux下C获取所有可用网卡信息
在Linux下开发网络程序时,经常会遇到需要取本地网络接口名.IP.广播地址.子网掩码或者MAC地址等信息的需求,最常见的办法是配合宏SIOCGIFHWADDR.SIOCGIFADDR.SIOCGIF ...
- Dockerfile命令
Dockerfile分基础镜像信息.维护者信息.镜像操作指令.容器启动时执行指令 FROM 镜像名:标签 第一条指令必须时FROM MAINTAINER 维护者信息 RUN command或者RUN ...
- JavaScript中的单引号和双引号解决
在使用JavaScript显示消息或者传递字符数据的时候,经常会碰到数据中夹杂单引号(')或者双引号("),这种语句往往会造成JavaScript报错.对此一般采用/'或者/"的解 ...
- java学习笔记13--反射机制与动态代理
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note13.html,转载请注明源地址. Java的反射机制 在Java运行时环境中,对于任意 ...
- tm标准mvc框架对应robotlegs 的mvc
tm标准mvc框架对应robotlegs 的mvc+s (其实都是一样样滴)
- 扩展JQUERY 表单加载JSON数据
$.fn.extend({ //表单加载json对象数据 setForm : function (jsonValue) { var obj = this; $.each(jsonValue, func ...
- Rust 格式输出
格式输出由一系列定义在 std::fmt 中的宏提供. 包含: format! : 输出格式化的字符串. print! : 输出格式化的字符串到控制台(终端)println!: 添加一个换行,输出格 ...
- minigui杂项
官方下载地址 MiniGUI简介 http://www.minigui.com/zhcn/download/ MiniGUI3.0.12 移植到mini2440 在海思hi3520上移植minigui ...