HDU 1026 Ignatius and the Princess I

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

题意就是从左上角走到右下角,输出用时最短的路径。必经路上有怪(数字代表打怪所花费的时间),必须打死才能过去。

这题是广度优先搜索,难点是路径的保存和输出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int vis[101][101];
int flag[101][101];
char map[101][101];
int f[4][2]= {0,1,1,0,0,-1,-1,0};
int n,m,fg,tim;
struct node
{
    int x,y,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
} pose;
void prim(int x,int y)///输出路线
{
    int nx,ny;
    if(!flag[x][y])///递归结束判断
        return;
    nx=x-f[flag[x][y]-1][0];
    ny=y-f[flag[x][y]-1][1];///计算它的前一步的坐标
    prim(nx,ny);///递归进去直到起点,然后递归返回输出路径
    printf("%ds:(%d,%d)->(%d,%d)\n",tim++,nx,ny,x,y);
    if(map[x][y]>'0'&&map[x][y]<='9')
    {
        int t=map[x][y]-'0';
        while(t--)
        {
            printf("%ds:FIGHT AT (%d,%d)\n",tim++,x,y);
        }
    }
}
void bfs()
{
    priority_queue<node>Q;
    pose.x=0;
    pose.y=0;
    pose.step=0;
    vis[0][0]=1;
    Q.push(pose);
    while(!Q.empty())
    {
        node p=Q.top();
        Q.pop();
        if(p.x==n-1&&p.y==m-1)
        {
            fg=1;
            cout<<"It takes "<<p.step<<" seconds to reach the target position, let me show you the way."<<endl;
            return;
        }
        for(int i=0; i<4; i++)
        {
            node tem;
            tem=p;
            tem.x+=f[i][0];
            tem.y+=f[i][1];
            if(tem.x<0||tem.y<0||tem.x>=n||tem.y>=m)
                continue;
            if(!vis[tem.x][tem.y]&&map[tem.x][tem.y]!='X')
            {
                vis[tem.x][tem.y]=1;
                if(map[tem.x][tem.y]=='.')
                    tem.step++;
                else
                    tem.step+=map[tem.x][tem.y]-'0'+1;
                flag[tem.x][tem.y]=i+1;///记录朝向,加1是为了输出递归0的因素
                Q.push(tem);
            }
        }
    }

}
int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        memset(flag,0,sizeof(flag));
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
                cin>>map[i][j];
        fg=0;
        bfs();
        if(fg)
        {
            tim=1;
            prim(n-1,m-1);
        }
        else
            cout<<"God please help our poor hero."<<endl;
        cout<<"FINISH"<<endl;
    }
}


你可能感兴趣的:(ACM,HDU,解题报告,广搜)