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

Ignatius and the Princess I

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题目简单翻译:

起始点为(0,0),终点为(n-1,m-1),数字代表有个怪物,该数字是怪物的血量,打掉怪物一滴血需要1s。求从起点到达终点的最短时间。如不能到达终点,输出”God please help our poor hero.”

解题思路:

BFS,优先队列。

每次取出最少时间的点进行操作。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
using namespace std;
struct node
{
    int x,y;
    int spent;
    int last;
    int now;
    int boss;
    bool operator <(const node &a)const
    {
        return spent>a.spent;//注意自己写的比较函数
    }
}St[120000];
priority_queue<node> pq;
int n,m;
char mp[120][120];
int vis[120][120];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

bool check(int x,int y)
{
    return x>=0&&x<n&&y>=0&&y<m;
}
void dfs(int a)
{
    if(a)
    {
        dfs(St[a].last);
        printf("%ds:(%d,%d)->(%d,%d)
",St[a].spent-St[a].boss,St[St[a].last].x,St[St[a].last].y,St[a].x,St[a].y);
        for(int i=St[a].boss-1;i>=0;i--)
        {
            printf("%ds:FIGHT AT (%d,%d)
",St[a].spent-i,St[a].x,St[a].y);
        }
    }
}
void output(int a)
{
    printf("It takes %d seconds to reach the target position, let me show you the way.
",St[a].spent);
    if(a)
        dfs(a);
}
bool bfs()
{
    memset(vis,0,sizeof vis);
    while(!pq.empty()) pq.pop();
    St[0].last=-1;
    St[0].spent=0;
    St[0].x=0;
    St[0].y=0;
    St[0].now=0;
    St[0].boss=0;
    int num=1;
    pq.push(St[0]);
    while(!pq.empty())
    {
        node e=pq.top();
        if(e.x==n-1&&e.y==m-1)
        {
            output(e.now);
            return true;
        }
        pq.pop();
        for(int i=0;i<4;i++)
        {
            int curx=e.x+dx[i];
            int cury=e.y+dy[i];
            if(check(curx,cury)&&vis[curx][cury]==0&&mp[curx][cury]!='X')
            {
                if(isdigit(mp[curx][cury])) St[num].boss=mp[curx][cury]-'0';
                else St[num].boss=0;
                vis[curx][cury]=1;
                St[num].x=curx,St[num].y=cury;
                St[num].spent=e.spent+1+St[num].boss;
                St[num].last=e.now;
                St[num].now=num;
                pq.push(St[num++]);
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%s",mp[i]);
        if(!bfs()) printf("God please help our poor hero.
");
        puts("FINISH");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/I-love-HLD/p/4625333.html