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


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

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

大致题意
给你一个n*m的迷宫,你在(0,0)点,需要走到(n-1,m-1)终点,迷宫里有障碍物‘X’,和小怪兽,每个小怪兽有个ph值>=1,用数字表示,你需要花ph的时间才能击败小怪兽通过该点。问你是否能到达终点,若能则输出最少需要的时间,和每秒的行动(参照样例),否则则输出“God please help our poor hero.”

思路:bfs+优先队列,保存路径输出

代码如下

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>

using namespace std;
int dix[4]={0,0,1,-1};
int diy[4]={1,-1,0,0};
char map[105][105];
int vis[105][105];

struct node{
    int x,y;//当前坐标
    int sx,sy;//上个位置的坐标
    int hp; //判断此位置是否有小怪物
    int time;//走到此位置花费的时间
    friend bool operator<(node a,node b)
    {
        return a.time>b.time; //用时少,优先度高
    }
};

node s[105][105];//保存路径。
int n,m,flag; 
priority_queue<node>que;//优先队列


void ru(node u)//回溯找路径,存入队列输出。 (还有种思路就是从终点开始向起点bfs然后存路径,这样就可以不用回溯,直接从起点输出路径。)
{
    if(u.sx==-1&&u.sy==-1)
    {
     que.push(u);
    return;
   }
    else 
    {
        que.push(u);
        ru(s[u.sx][u.sy]);
    }
}

void bfs()
{
    while(!que.empty())
    {
        node u=que.top();
        que.pop();
        if(u.x==n-1&&u.y==m-1)//到达终点
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",u.time);

            while(!que.empty())
            que.pop();//将队列先清空,以便保存回溯找到的路径
            ru(u);//回溯找路径
            int T=0;
            node k1,k2;
            while(!que.empty())//输出队列中保存的路径
            {
                if(T==0)
                {
                    k1=k2=que.top();
                    que.pop();
                    T++;
                }
                else 
                {
                    k1=k2;
                    k2=que.top();
                    que.pop();
                    if(k2.hp==0)
                    {
                        printf("%ds:(%d,%d)->(%d,%d)\n",T,k1.x,k1.y,k2.x,k2.y);
                        T++;
                    }
                    else
                    {
                        printf("%ds:(%d,%d)->(%d,%d)\n",T,k1.x,k1.y,k2.x,k2.y);
                        T++;
                        while(k2.hp--)
                        {
                            printf("%ds:FIGHT AT (%d,%d)\n",T,k2.x,k2.y);
                            T++;
                        }
                    }
                }

            }
            flag=1;
            return;
        }

        for(int i=0;i<4;i++)//普通的bfs
        {
            int x=u.x+dix[i];
            int y=u.y+diy[i];
            if(map[x][y]!='X'&&vis[x][y]==0&&x>=0&&x<n&&y>=0&&y<m)
            {
                if(map[x][y]=='.')
                {
                    node v;
                    v.sx=u.x;
                    v.sy=u.y;
                    v.x=x;
                    v.y=y;
                    v.hp=0;
                    v.time=u.time+1;
                    s[x][y]=v;
                    vis[x][y]=1;
                    que.push(v);
                }
                else 
                {
                    node v;
                    v.sx=u.x;
                    v.sy=u.y;
                    v.x=x;
                    v.y=y;
                    v.hp=map[x][y]-'0';
                    v.time=u.time+1+v.hp;
                    s[x][y]=v;
                    vis[x][y]=1;
                    que.push(v);
                }
            }
        }
     } 
}

int main(void)
{

    while(cin>>n>>m)
    {
        while(!que.empty())
        que.pop();
        memset(vis,0,sizeof(vis));
        getchar();
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        cin>>map[i][j];
        node u;
        u.sx=u.sy=-1;
        u.x=0;
        u.y=0;
        u.time=0;
        u.hp=0;
        vis[0][0]=1;

        s[0][0]=u;
        que.push(u);
        flag=0;
        bfs();
        if(flag==0)
        cout<<"God please help our poor hero.\n";
        cout<<"FINISH\n";
    }
    return 0;
}
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告