HDU Ignatius and the Princess I

来源:互联网 发布:mac版ps怎么导入字体 编辑:程序博客网 时间:2024/06/05 17:41
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

这道题的意思就是找最短时间完成的,还要还原路径,最短路当然会想到bfs啦,可是这里不是单纯地求最短路的问题,这里我们用到优先队列,以时间短的为优先。

至于路径就用一个数组来维护。用栈逆序输出。

不然你也可以寻找从终点到起点的最短时间,这样就不用用到栈啦,不过终点可能有妖怪,记得加上终点打妖怪的所耗费的时间。


#include<iostream>#include<cmath>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#include<ctime>#include<string>#include<stack>#include<deque>#include<queue>#include<list>#include<set>#include<map>#include<cstdio>#define mes(x) memset(x, 0, sizeof(x))#define Pii pair<int, int>#define Pll pair<ll, ll>#define INF 1e9+7typedef long long ll;using namespace std;const int MAX = 10;#define MOD 1000000007#define test#define timestruct node{    int x;    int y;    int _time;    friend bool operator<(node  a, node b)  //设置  时间短的优先级大    {        return a._time>b._time;    }};int n, m, gx, gy, res;int dx[4] = {0, 0, 1 ,-1};  //方向, 上下左右int dy[4] = {1, -1, 0, 0};char mp[102][102];node tmp[102][102];  //记录路径bool visit[102][102];  //记录是否走过int bfs(){    priority_queue<node> que;      node current, next;  //curren表示当前步, next便是下一步    current.x = 0;   //起点    current.y = 0;    current._time = 0;    visit[current.x][current.y] = true;  //原地不能再走    que.push(current);      while(!que.empty()){        current = que.top();        que.pop();        if(current.x == gx && current.y == gy){  //最短时间            return current._time;        }        for(int i = 0; i < 4; ++i){  //四个方向遍历            next.x = current.x + dx[i];               next.y = current.y + dy[i];            if(next.x < n && next.x >= 0 && next.y < m && next.y >= 0 && mp[next.x][next.y] != 'X' && !visit[next.x][next.y]){//可以走                next._time = current._time + 1;                 if(mp[next.x][next.y]>= '1' && mp[next.x][next.y] <= '9'){                    next._time = next._time + (mp[next.x][next.y] - '0');  //碰到敌人,加上打敌人所耗费的时间                }                visit[next.x][next.y] = true;                  tmp[next.x][next.y].x = current.x;  //记录下一步的前一步的坐标                tmp[next.x][next.y].y = current.y;                tmp[next.x][next.y]._time = next._time;  //时间                que.push(next);            }        }    }    return -1;}void play_the_path()  //还原路劲  {    stack<node> st;    node current;    current.x = n - 1;    current.y = m - 1;    current._time = tmp[n - 1][m - 1]._time;    while(current.x != 0 || current.y != 0){ //记住是都为(0, 0)才是结束条件        st.push(current);        current = tmp[current.x][current.y];    }   // st.push(current);    int __time = 0;    while(!st.empty()){ //输出路径        current = st.top();        st.pop();        if(mp[current.x][current.y] == '.'){            printf("%ds:(%d,%d)->(%d,%d)\n", ++__time, tmp[current.x][current.y].x, tmp[current.x][current.y].y, current.x, current.y);        }        else{            printf("%ds:(%d,%d)->(%d,%d)\n", ++__time, tmp[current.x][current.y].x, tmp[current.x][current.y].y, current.x, current.y);            int n = mp[current.x][current.y] - '0';            while(n--){                printf("%ds:FIGHT AT (%d,%d)\n", ++__time, current.x, current.y);            }        }    }}int main(){#ifdef test    freopen("/home/ostreambaba/文档/input.txt", "r", stdin);  //freopen("/home/ostreambaba/文档/output.txt", "w", stdout);#endif    while(cin >> n >> m)    {        for(int i = 0; i < n; ++i){//输入            for(int j = 0; j < m; ++j){                cin >> mp[i][j];            }        }        memset(visit, false, sizeof(visit));        gx = n-1, gy = m-1;        res = bfs();        if(res != -1){            printf("It takes %d seconds to reach the target position, let me show you the way.\n", res);            play_the_path();        }        else{           printf("God please help our poor hero.\n");        }        printf("FINISH\n");    }    return 0;}


0 0
原创粉丝点击