hdu 1026 Ignatius and the Princess I(优先队列+BFS)

来源:互联网 发布:获取股票实时数据 编辑:程序博客网 时间:2024/05/16 16:19

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10917    Accepted Submission(s): 3332
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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH
 

Author
Ignatius.L
 
题目大意:
有一个m*n的迷宫,起点是左上角(坐标0 ,0) ,终点是右下角(坐标m-1,n-1)。在迷宫中有若干个怪兽,当你在行进的过程中碰到怪兽,你就必须杀死他,怪兽有hp,杀死一个怪兽所需的时间就是等于他的hp。每走一格所需要的时间是一秒。现在要求,如果能走出迷宫,则把所需的最短时间输出来,而且还要把路径写出来(其中包括打怪兽的状态)。否则,输出“God please help our poor hero.”。
解题思路:
看见这种迷宫类最短路径问题,就应该想到BFS,但是其中每个格子的状态不一定相同,因为有怪兽。所以我们应该想到还要用优先队列。有了这两个想法,就差不多能解决这个问题了,但是还要打印路径,所以就用一个结构体专门保存路径。为了让路径更好的记录下来,所以我选择从终点开始BFS,一直到起点。
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<string>#include<stack>#include<queue>#include<vector>#include<algorithm>#include<iostream>using namespace std;#ifdef __int64typedef __int64 LL;#elsetypedef long long LL;#endif#define maxn 150int m,n;int cnt;int dir[4][2]={1,0,-1,0,0,1,0,-1};char v[maxn][maxn];int vis[maxn][maxn];int fight[maxn][maxn];//记录打怪兽的点,和所需的时间struct point{    int x,y,time;    friend bool operator<(const point &a,const point &b)//改变优先级,使时间小的优先级高    {        return a.time>b.time;    }};struct _MAP{    int x,y;}mp[maxn][maxn];//保存路径,记录每个点的前驱void bfs(){    priority_queue<point >q;    point now,next;    now.x=m-1;now.y=n-1;//从终点开始记录,这样便于路径的保存和打印    if(v[now.x][now.y]>='1'&&v[now.x][now.y]<='9')    {        now.time=v[now.x][now.y]-'0';        fight[now.x][now.y]=v[now.x][now.y]-'0';    }    else now.time=0;    q.push(now);    while(!q.empty())    {        now=q.top(); q.pop();        if(now.x==0&&now.y==0)        {            cnt=now.time;            return ;        }        for(int i=0;i<4;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&!vis[next.x][next.y]&&v[next.x][next.y]!='X')            {                vis[next.x][next.y]=1;                mp[next.x][next.y].x=now.x;                mp[next.x][next.y].y=now.y;                if(v[next.x][next.y]>='1'&&v[next.x][next.y]<='9')//遇到怪兽                {                    fight[next.x][next.y]=v[next.x][next.y]-'0';                    next.time=now.time+1+fight[next.x][next.y];                }                else next.time=now.time+1;                q.push(next);            }        }    }}int main(){    while(~scanf("%d%d",&m,&n))    {        for(int i=0;i<m;i++)        {            scanf("%s",v[i]);        }        memset(vis,0,sizeof(vis));        memset(fight,0,sizeof(fight));        vis[m-1][n-1]=1;        cnt=0;        bfs();        if(cnt)        {            printf("It takes %d seconds to reach the target position, let me show you the way.\n",cnt);            int step=1;            int tx=0,ty=0;            while(step<=cnt)            {                int fx=mp[tx][ty].x,fy=mp[tx][ty].y;                printf("%ds:(%d,%d)->(%d,%d)\n",step++,tx,ty,fx,fy);                for(int i=1;i<=fight[fx][fy];i++)//如果该点有怪兽,就应该把打怪兽的状态输出来                    printf("%ds:FIGHT AT (%d,%d)\n",step++,fx,fy);                tx=fx;ty=fy;            }        }        else        {            printf("God please help our poor hero.\n");        }        printf("FINISH\n");    }    return 0;}


0 0