注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

[hdu]1026 Ignatius and the Princess I  

2012-08-21 00:45:40|  分类: HDU |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
一题Bfs的题目,并要求记录每一步。
很经典的题目。
题目放出:

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7228    Accepted Submission(s): 2162
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
 

Author
Ignatius.L



代码:



#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
char
map[101][101];
int
vis[101][101],n,m,mins;
bool
rescue;
int
dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct
node
{

int
x,y,cnt;
node(int _x=0,int _y=0,int _cnt=0):x(_x),y(_y),cnt(_cnt){};
friend
bool operator <(const node &a, const node &b)
{

return
a.cnt>b.cnt;
}
};

node f[101][101];
void
bfs()
{

node t;
t.x=1;t.y=1;t.cnt=0;
priority_queue<node> Q;
Q.push(t);
vis[1][1]=1;
while
(!Q.empty())
{

node b=Q.top();
Q.pop();
if
(b.x==n&&b.y==m)
{

rescue=1;
mins=b.cnt;
return
;
}

for
(int k=0;k<4;k++)
{

int
i=b.x+dir[k][0];
int
j=b.y+dir[k][1];
if
(i<=n&&i>0&&j<=m&&j>0&&!vis[i][j]&&map[i][j]!='X')
{

vis[i][j]=1;
f[i][j].x=b.x;f[i][j].y=b.y;f[i][j].cnt=b.cnt+1;//记录前驱
if(map[i][j]=='.')
Q.push(node(i,j,b.cnt+1));
else
Q.push(node(i,j,b.cnt+(map[i][j]-'0')+1));
}
}
}
}

void
print()
{

stack<node> S;
node temp=f[n][m];
S.push(node(n,m,mins));
while
(temp.x!=1||temp.y!=1)
{

S.push(temp);
temp=f[temp.x][temp.y];
}

int
t=1;
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-1,f[temp.x][temp.y].y-1,temp.x-1,temp.y-1);
else
{
printf("%ds:(%d,%d)->(%d,%d)\n",t++,f[temp.x][temp.y].x-1,f[temp.x][temp.y].y-1,temp.x-1,temp.y-1);
int
k=map[temp.x][temp.y]-'0';
while
(k--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,temp.x-1,temp.y-1);
}
}

printf("FINISH\n");
return
;
}


int
main()
{

while
(cin>>n>>m)
{

for
(int i=1;i<=n;i++)
{

for
(int j=1;j<=m;j++)
cin>>map[i][j];
}

memset(vis,0,sizeof(vis));
rescue=0;
bfs();
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();

}

return
0;
}

/*

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

*/


  评论这张
 
阅读(236)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018