您的位置:首页 > 其它

HDU 1026 Ignatius and the Princess I (BFS输出路径)

2013-08-12 16:12 417 查看

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9934 Accepted Submission(s): 2972
Special Judge


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]

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.

题意:从(0,0)点到地图最右下角的最少步数,并把路径输出。使用优先队列求少步数+路径记录数组father[]标记。

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
#include<conio.h>
using namespace std;
stack<int> s;
int vis[103][104];
char map[102][102];
int father[10003];
int Row,Column,Char_Num;
int Dir[][2] = {-1,0,0,1,1,0,0,-1};
struct Point
{
int x,y;
int time;
bool operator < (const Point &a)const{
return a.time < time;
}
};
int Time(char c)
{
if( c<='9' && c>='0')return c-'0';
return 0;
}
bool Check(int x,int y)
{
if( x<0 || x>=Row || y<0 || y>=Column || map[x][y] == 'X' || vis[x][y])
return 0;
return 1;
}
void Prin(int T)
{
int i,n,t=1,N = Row * Column - 1;
while( N != 0){
s.push(N);//入栈
if( (n = Time(map[N/Column][N%Column])) )
for(i=1;i<=n;i++)
s.push(N);//该点是数字,则入栈相应次数
N = father
;//上一个节点
}
printf("It takes %d seconds to reach the target position, let me show you the way.\n",T);
N = s.top(); s.pop();
printf("%ds:(0,0)->(%d,%d)\n",t++,N/Column,N%Column);
while( !s.empty())
{
if(N == s.top())
printf("%ds:FIGHT AT (%d,%d)\n",t++,N/Column,N%Column);
else
printf("%ds:(%d,%d)->(%d,%d)\n",t++,N/Column,N%Column,s.top()/Column,s.top()%Column);
N = s.top();
s.pop();
}
}
int BFS(int x,int y)
{
int T;
Point Now_Q,Next_P;
priority_queue<Point> Que;
Next_P.x = x; Next_P.y = y; Next_P.time = 0;
vis[x][y] = 1;
Que.push(Next_P);
while( !Que.empty() )
{
Now_Q = Que.top();
Que.pop();
if(Now_Q.x == Row-1 && Now_Q.y == Column-1){//找到输出
Prin(Now_Q.time);
return 1;
}
for(int i=0;i<4;i++){
Next_P.x = Now_Q.x + Dir[i][0];
Next_P.y = Now_Q.y + Dir[i][1];
if( Check(Next_P.x, Next_P.y)){
father[Next_P.x*Column + Next_P.y ] = Now_Q.x*Column + Now_Q.y;//标记当前节点的上一个节点
Next_P.time = Now_Q.time + 1;
if( (T = Time(map[Next_P.x][Next_P.y])) ){//该点是数字,时间增加相应数字
Next_P.time += T;
}
Que.push(Next_P);
vis[Next_P.x][Next_P.y] = 1;
}
}
}
return 0;
}
int main()
{
int i,j;
while(~scanf("%d%d",&Row,&Column))
{
getchar();
for(i=0;i<Row;i++)
for(j=0;j<Column;j++){
cin>>map[i][j];
}
memset(vis,0,sizeof(vis));
memset(father,0,sizeof(father));
while( !s.empty())s.pop();
if( !BFS(0,0) )
printf("God please help our poor hero.\n");
puts("FINISH");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: