HDU 1026 Ignatius and the Princess I

来源:互联网 发布:2016年8月非农数据信息 编辑:程序博客网 时间:2024/05/22 01:35

HDU 1026 Ignatius and the Princess I

题目链接

题目:

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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH

解题思路:
这道题目是典型的广度优先搜索,遍历所有路径求出最短路经。但是这道题必须优化,要不然会超时。优化方案是建立一个arr[n]数组每次记录通过的这个Map[x][y]到目的地时间保证是小于上次遍历,这样可以省很多时间。题目也要求输出耗时最短的路径,可以建立一个前置节点,保存Now[x][y]前 的节点,然后递归推出。
BFS 一般用队列实现,这里直接用STL中的队列。
代码:

// 江流儿.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <stdio.h>#include <queue>#include <stack>using namespace std;int Move[4][2]={{0,1},{0,-1},{1,0},{-1,0}};char Map[105][105];int Hash[105][105];typedef struct{    int x,y;    int time,n;    int prex,prey;}Point;typedef struct{    int x,y;    int n;}Road;Point Now,Next,End;Road road;const int Max=99999;int minSecond;queue <Point> Q; stack <Point> Output;stack <Road> S;int main(){    int N,M;    while(cin>>N>>M)    {        minSecond=Max;        for(int i=0;i<N;i++)        {            for(int j=0;j<M;j++)            {                cin>>Map[i][j];                Hash[i][j]=Max;            }        }        Now.time=0; Now.n=0;        Now.x=0 ;Now.y=0;        Now.prex=0;Now.prey=0;         Q.push(Now);        Map[Now.x][Now.y]='X';        while(!Q.empty())        {            Now=Q.front();            Q.pop();            if(Map[Now.x][Now.y]>='1' && Map[Now.x][Now.y]<='9')                Now.time+=Map[Now.x][Now.y]-'0';            if(Now.x==N-1 && Now.y==M-1) //到达终点            {                if(Now.time<minSecond)                    minSecond=Now.time;                continue;            }            for(int i=0;i<4;i++)            {                Next.x=Now.x+Move[i][0];                Next.y=Now.y+Move[i][1];                Next.prex=Now.x;  //保存下一节点的父节点,后便于递归输出路径                Next.prey=Now.y;                Next.time=Now.time+1;                Next.n=0;                //Hash[Next.x][Next.y]>Next.time这段代码是关键                if(Next.x>=0&& Next.y>=0&&Next.x<N&&Next.y<M && Map[Next.x][Next.y]!='X')//题目约束条件                {                    if(Map[Next.x][Next.y]>='1' && Map[Next.x][Next.y]<='9')                         Next.n=Map[Next.x][Next.y]-'0';;                     if(Hash[Next.x][Next.y]>Next.time)                    {                        Hash[Next.x][Next.y]=Next.time;                        Q.push(Next);                        Output.push(Next);                    }                }            }         }        if(minSecond!=Max)           printf("It takes %d seconds to reach the target position, let me show you the way.\n",minSecond);        int prex=N-1,prey=M-1;        while(!Output.empty())  //递归回去求路径        {            End=Output.top();            if(prex!=End.x|| prey!=End.y)            {                Output.pop();                continue;            }            prex=End.prex;            prey=End.prey;            road.x=End.x;            road.y=End.y;            road.n=End.n;            S.push(road);            Output.pop();        }        road.x=0;        road.y=0;        road.n=0;        S.push(road);        int time=0;        while(!S.empty())        {            road=S.top();            S.pop();            if(S.empty())                break;            while(road.n--)                printf("%ds:FIGHT AT (%d,%d)\n",++time,road.x,road.y);            printf("%ds:(%d,%d)->(%d,%d)\n",++time,road.x,road.y,S.top().x,S.top().y);        }        while(road.n--)                printf("%ds:FIGHT AT (%d,%d)\n",++time,road.x,road.y);        if(minSecond==Max)            cout<<"God please help our poor hero."<<endl;        cout<<"FINISH"<<endl;      }    return 0;}

运行截图:

这里写图片描述

AC截图:

这里写图片描述

0 0