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

说实话,看到样例就不想写的题目。。。。

题意是给出迷宫,输入行和列,row,cow(原谅我的列喜欢用cow表示),要求从(0,0)走到(row-1,cow-1);
也就是从左上角走到右下角
每走一步花一秒,'.'表示可以走'X'表示不能走,'n'(n是1-9的数字)表示在这点战斗,战斗用时n
求最短时间和路径
显然是优先队列加输出路径
类似有POJ的3984也是要输出路径的BFS,不过那题是普通队列要简单得多

//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define sqrt(n) sqrt((double)n)
#define pow(a,b) pow((double)a,(int)b)
#define inf 1<<29
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
int row,cow;
char mp[104][104];
char note[104][104];
void getmap()
{
    for (int i = 0; i < row; ++i)
    {
        scanf("%s",mp[i]);
        strcpy(note[i],mp[i]);
    }
}
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
struct Node
{
    int x, y, t;
    inline bool operator < (const Node &a) const
    {
        return t > a.t;//时间小的优先出队
    }
};
struct Path
{
    int x,y;
} p[104][104]; //p[i][j]的前一个坐标是(p[i][j].x,p[i][j].y)
int ans;
bool bfs()
{
    priority_queue<Node>q;
    Node s ;
    s.x = 0,s.y = 0,s.t = 0;//起点不会有数字
    q.push(s);
    p[0][0].x = -1,p[0][0].y = -1;
    while (!q.empty())
    {
        Node h = q.top();
        q.pop();
        if (h.x == row-1&&h.y == cow-1)
        {
            ans = h.t;
            return 1;
        }
        for (int i = 0; i < 4; ++i)
        {
            Node nx;
            nx.x = h.x + dx[i];
            nx.y = h.y + dy[i];
            if (nx.x>=0&&nx.y>=0&&nx.x<row&&nx.y<cow)
            {
                if (mp[nx.x][nx.y] == '.')
                {
                    mp[nx.x][nx.y] = 'X';
                    nx.t = h.t+1;
                    q.push(nx);
                    p[nx.x][nx.y].x = h.x;
                    p[nx.x][nx.y].y = h.y;
                }
                if (isdigit(mp[nx.x][nx.y]))
                {
                    nx.t = h.t+1+mp[nx.x][nx.y]-48;
                    q.push(nx);
                    mp[nx.x][nx.y] = 'X';
                    p[nx.x][nx.y].x = h.x;
                    p[nx.x][nx.y].y = h.y;
                }
            }
        }
    }
    return 0;
}
int kas;
void print(int x,int y)
{
    if (x == 0&& y == 0) return ;
    print(p[x][y].x,p[x][y].y);
    printf("%ds:(%d,%d)->(%d,%d)\n",kas++,p[x][y].x,p[x][y].y,x,y);
    if (isdigit(note[x][y]))
    {
        int w = note[x][y] - 48;
        for (int i = 0;i < w;++i)
            printf("%ds:FIGHT AT (%d,%d)\n",kas++,x,y);
    }
}
void test()
{
    int i = row-1,j = cow-1;
    while (1)
    {
        if (i==0&&j==0) break;
        cout<<i<<"---"<<j<<endl;
        int xx = p[i][j].x, yy = p[i][j].y;
        cout<<xx<<"???"<<yy<<endl;
        i = xx,j = yy;
    }
}
int main()
{
    while (cin>>row>>cow)
    {
        getmap();
        if (bfs())
        {
            //test();
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
            kas = 1;
              print(row-1,cow-1);
        }
        else
        {
            puts("God please help our poor hero.");
        }
        puts("FINISH");
    }
    return 0;
}


你可能感兴趣的:(优先队列,杭电,bfs)