HDU 1026 Ignatius and the Princess I

7 篇文章 0 订阅
5 篇文章 0 订阅

题目

hduoj1026
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

一、题目大意

公主被魔王凤5166绑架了,我们的英雄伊格纳修斯要去救我们美丽的公主。现在他进入了丰5166的城堡。城堡是一个巨大的迷宫。为了简化问题,我们假设迷宫是一个N*M的二维阵列,其左上角为(0,0),右下角为(N-1,M-1)。伊格纳修斯在(0,0)进入,冯5166房间的门在(N-1,M-1),这是我们的目标。城堡里有一些怪物,如果伊格纳修斯遇到他们,他必须杀死他们。以下是一些规则:
1.伊格纳修斯只能向四个方向移动(上、下、左、右),每秒一步。一步定义如下:如果当前位置是(x,y),那么在一步之后,伊格纳修斯只能站在(x-1,y)、(x+1,y)、(x,y-1)或(x,y+1)上。
2.数组中有一些字符和数字。我们这样定义它们:
. :伊格纳修斯可以行走的地方。
X:这个地方是个陷阱,伊格纳修斯不应该在上面走。
n:这是一个有n血量(1<=n<=9)的怪物,如果伊格纳修斯在上面行走,他需要n秒才能杀死这个怪物。
你的任务是给出伊格纳修斯到达目标位置的最短时间路径。你可以假设起始位置和目标位置永远不会是陷阱,并且起始位置永远不会有怪物。

二、代码

这题是一个迷宫类型的广搜题,比较难的地方是它的输出,因为这题要输出路径
(参考了一下别人的代码)

代码如下(示例):

#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
    int x,y;
    int step;
    //优先队列里边的排序规则
    friend bool operator<(node n1,node n2)
    {
        return n2.step<n1.step;//升序
    }
};
int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
int map[111][111];
int flag[111][111];
int blood[111][111];
int n,m;
int roarNumber;

int judge(int x,int y)
{
    //判断边界条件
    if(x<0 || x>=n || y<0 || y>=m)	return 1;
    if(map[x][y]==-1)				return 1;
    return 0;
}
//1.定义一个优先队列,将定义两个节点:cur,next
//2.将起始坐标点赋值给cur节点,map[0][0]=-1(表示起点已走过),将cur节点压入队列
//3.用while循环开始搜索,当走到右下角时返回步数
//4.朝四个方向广搜
int BFS()
{
    //定义一个优先队列
priority_queue<node>q;
node cur,next;
cur.x=0;
cur.y=0;
cur.step=0;
map[0][0]=-1;
q.push(cur);
    //初始坐标点(0,0)

    while(!q.empty())
    {
        cur=q.top();
        q.pop();

        //走到右下角
        if(cur.x==n-1&&cur.y==m-1)
            return cur.step;
        //四个方向广搜
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];

            //如果这个点不符合条件的话 直接跳过下边的步骤进行循环
            if(judge(next.x,next.y))
                continue;

            //记录下一个位置的步数 map里边存放的是这个点需要停留的时间
             next.step=cur.step+1+map[next.x][next.y];
            //记录从哪个方向来到这个点的
            flag[next.x][next.y]=i+1;
            // 标记一下这个点走过了
            map[next.x][next.y]=-1;
            q.push(next);
        }
    }
    return -1;
}
void road(int x,int y)
{
    //用递归的方式查找
    int next_x,next_y;
    //右下角的点最后会是0
    if(flag[x][y]==0)
        return ;
    next_x=x-dir[flag[x][y]-1][0];
    next_y=y-dir[flag[x][y]-1][1];
    road(next_x,next_y);
    printf("%ds:(%d,%d)->(%d,%d)\n",roarNumber++,next_x,next_y,x,y);

    while(blood[x][y]--)
    {
        //输出停留的时间
        printf("%ds:FIGHT AT (%d,%d)\n",roarNumber++,x,y);
    }
}

int main()
{
    char str[111];
    int i,l;
    int ans;

    while(scanf("%d%d",&n,&m)!=-1)
    {
        memset(map,0,sizeof(map));
        memset(flag,0,sizeof(flag));
        memset(blood,0,sizeof(blood));
        for(i=0;i<n;i++)
        {
            scanf("%s",str);
            for(l=0;str[l];l++)
            {
                if(str[l]=='.')
                {
                    //不需要停留
                    map[i][l]=0;
                }
                else
                {
                    if(str[l]=='X')
                    {
                        //不能走
                        map[i][l]=-1;
                    }
                    else
                    {
                        //需要停留
                        map[i][l]=blood[i][l]=str[l]-'0';
                    }
                }
            }
        }

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

            roarNumber=1;
            road(n-1,m-1);
        }
        printf("FINISH\n");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值