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 ( ...
随机推荐
- Delphi 6 保存窗体设置
DSK Desktop Setting File 保存工程文件的桌面摆布情况, 下次打开时可以恢复上次保存的桌面状态 Desktop文件.保存了IDE的布局(也可能包含浏览记号,视乎IDE的设定),为 ...
- #include <NOIP2010 Junior> 三国游戏 ——using namespace wxl;
题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之 ...
- iOS:三种数据库的小总结
三种数据库总结:sqlite.FMDB.CoreData 1.sqlite数据库(C语言)需要方法和属性: (1)数据类型: –INTEGER 有符号的整数类型 –REAL 浮点类型 –TEXT ...
- Node.js:get/post请求、全局对象、工具模块
一.GET/POST请求 在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交.表单提交到服务器一般都使用 GET/POST 请求. 1.获取GET请求内容 由于GET请求直接被嵌入在路径 ...
- .NET-MVC站点部署到windows server2008r2服务器404错误
iis站点搭建 产生原因: 由于服务器上的.net4.0没有进行注册导致的 解决方法: 注册.net 4.0 打开运行-cmd-输入如下命令: C:\WINDOWS\Microsoft.NET\Fra ...
- Download Visual Studio
Welcome to a new way to install Visual Studio! In our newest version, we've made it easier for you t ...
- Ajax datatype:'JSON'的error问题Status1:200,JSON格式
转自:http://blog.sina.com.cn/s/blog_6e001be701017rux.html <script src="../js/jquery-1.8.0-vsdo ...
- (剑指Offer)面试题13:在O(1)时间内删除链表结点
题目: 在给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点.链表结点与函数的定义如下: struct ListNode{ int val; ListNode* next; } ...
- SELECT语句逻辑运行顺序,你知道吗?
引言 这不是一个什么多深的技术问题.多么牛叉的编程能力.这跟一个人的开发能力也没有很必定的直接关系,可是知道这些会对你的SQL编写,排忧及优化上会有很大的帮助.它不是一个复杂的知识点.可是一个很基础的 ...
- source insight中{}自动缩进的调整
默认的自动缩进非常难看,解决方法如下: 菜单栏 -> Options -> document options ->点击右侧的 “Auto Indent...”按钮 将右侧" ...