HDU 1026 Ignatius and the Princess I

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.
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

解题思路:
这道题目是典型的广度优先搜索,遍历所有路径求出最短路经。但是这道题必须优化,要不然会超时。优化方案是建立一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值