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

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
 #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 最小步数,并且保存路径的更多相关文章

  1. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. HDOJ.1029 Ignatius and the Princess IV(map)

    Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...

  7. HDOJ 1028 Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  9. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

随机推荐

  1. sed替换换行符“\n”

    linux sed命令,如何替换换行符“\n” 在一次sed使用中,执行命令: sed "s/\n//g" file 1 发现,没起到任何效果. 后来,经查sed官方用户手册,才得 ...

  2. uva 816 abbott&#39;s revenge ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r

  3. 用mappedbytebuffer实现一个持久化队列【转】

    自从前段时间的一个事故让队列里缓存的大量关键数据丢失后,一直琢磨着弄一个能持久化到本地文件的队列,这样即使系统再次发生意外,我也不至于再苦逼的修数据了.选定使用mappedbytebuffer来实现, ...

  4. 构建高可用Linux服务器一

    1.显示物理CPU个数:cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -1 2.显示每个物理CPU中的core ...

  5. netlink error: too many arguments to function 'netlink_kernel_create'

    2.6版本的 netlink_kernel_create(&init_net, NETLINK_TEST, 0, NULL, kernel_receive ,THIS_MODULE);   3 ...

  6. SELECT语句逻辑运行顺序,你知道吗?

    引言 这不是一个什么多深的技术问题.多么牛叉的编程能力.这跟一个人的开发能力也没有很必定的直接关系,可是知道这些会对你的SQL编写,排忧及优化上会有很大的帮助.它不是一个复杂的知识点.可是一个很基础的 ...

  7. linux&amp;shell

    Linux经常使用命令 登录时显示信息放在/etc/motd和/etc/profile.d/xxx.sh motd放置字符串 profile.d下放置脚本文件 echo echo -e 处理特殊字符. ...

  8. UI组件之TextView及其子类(五)计时器Chronometer

    Chronometer直接继承了TextView组件,它会显示一段文本,显示从某个事实上时间開始.一共过了多长时间.我们看Chronometer的源代码: watermark/2/text/aHR0c ...

  9. Android-PullToRefresh(一)

    先讲下这篇写啥东西,也就是这家伙(chrisbanes)写的一个上拉下拉刷新的Demo,连接https://github.com/fengcunhan/Android-PullToRefresh 东西 ...

  10. Zookeeper研究和应用

    http://www.searchtb.com/2011/01/zookeeper-research.html zookeeper简介 zookeeper是一个开源分布式的服务,它提供了分布式协作,分 ...