题目

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

题解

这道题牵扯到了权值

因此,不能单纯地使用BFS

在牵扯权值时,我们要不断走最短的路,使用dijkstra算法

以怪物的HP+1(移动耗时+战斗耗时)为权值(没有怪物权值为1)

由于最后要输出路径, 因此每次进行松弛操作(更新距离)要记录下最短的路径

最后输出需要正序输出,因此可使用栈

代码

/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)


const int maxn = 155;

int N,M;
char Map[maxn][maxn];
pair<int,int> last[maxn][maxn];
int dis[maxn][maxn];

struct point {
    int x,y;
    int dis;
    point(int a,int b,int c) {
        x = a;
        y = b;
        dis = c;
    }
    bool operator < (const point &rhs)const {
        return dis > rhs.dis;
    }
};


const int delta[] = {1,-1,0,0};

int BFS() {
    priority_queue<point> Q;
    bool visited[maxn][maxn];
    memset(visited,false,sizeof(visited));
    memset(dis,-1,sizeof(dis));

    Q.push(point(0,0,0));
    dis[0][0] = 0;

    while (!Q.empty()) {

        int th1 = Q.top().x;
        int th2 = Q.top().y;
        int thdis = Q.top().dis;
        Q.pop();

        if (visited[th1][th2] == true)
            continue;
        visited[th1][th2] = true;

        //达到终点
        //if (th1 == N - 1 && th2 == M - 1)
        //    return dis[N - 1][M - 1];

        //拓展节点
        REP(4) {
            int next1 = th1 + delta[o];
            int next2 = th2 + delta[3 - o];

            if (Map[next1][next2] != 'X' && next1 >= 0
                && next1 < N && next2 >= 0 && next2 < M) {

                int weight;
                if (Map[next1][next2] == '.')
                    weight = 1;
                else
                    weight = Map[next1][next2] - '0' + 1;

                int temp = dis[next1][next2];

                dis[next1][next2] = dis[next1][next2] == -1 ?
                    dis[th1][th2] + weight :
                    min(dis[th1][th2] + weight,dis[next1][next2]);

                if (temp != dis[next1][next2]) {//记录路径
                    last[next1][next2] = pair<int,int>(th1,th2);
                    //printf("%d %d -> %d %d\n", th1, th2, next1, next2);
                }

                Q.push(point(next1,next2,dis[next1][next2]));

            }

        }

    }
    if (dis[N - 1][M - 1])
        return dis[N - 1][M - 1];
    else
        return -1;
}

bool Do() {
    if (scanf("%d%d",&N,&M) == EOF)
        return false;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            scanf("\n%c\n",&Map[i][j]);
    /*
    for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++)
    printf("%c", Map[i][j]);
    printf("\n");
    }
    */

    if (BFS() == -1) {
        printf("God please help our poor hero.\nFINISH\n");
    } else {
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",dis[N - 1][M - 1] );

        stack<pair<int,int> > s;
        int x = N - 1,y = M - 1;
        while (!(x == 0 && y == 0)) {
            //printf("s %d %d\n", x, y);
            s.push(pair<int,int>(x,y));
            pair<int,int> t = last[x][y];
            x = t.first;
            y = t.second;
        }
        //s.pop();
        //s.push(point(0, 0));

        REP(dis[N - 1][M - 1]) {
            printf("%ds:",o + 1);

            x = s.top().first;
            y = s.top().second;
            s.pop();
            printf("(%d,%d)->(%d,%d)\n",last[x][y].first,last[x][y].second,x,y);

            if (Map[x][y] >= '0'&&Map[x][y] <= '9') {
                int wait = Map[x][y] - '0';
                for (o++; wait; wait--,o++)
                    printf("%ds:FIGHT AT (%d,%d)\n",o + 1,x,y);
                o--;
            }


        }
        printf("FINISH\n");

    }



    return true;
}

int main() {
    while (Do());
    return 0;
}