HDU:1026 Ignatius and the Princess

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17851    Accepted Submission(s): 5719
 Special Judge

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
题目翻译:
题目描述: 国王被BEelzebub feng5166 abducted了,我们的英雄Ignatius要去救我们可爱的国王。现在他到达了feng5166的城堡,城堡是一个巨大的labyrinth.为了简化问题,我们假设labyrinth是一个N*M型的二维迷宫,左上角坐标为(0,0),右下角坐标为 (N-1,M-1),Ignatius从(0,0)进去,feng5166的房间在(N-1,M-1)这个位置,这是我们的目标,城堡中有一些妖怪,如果他遇到妖怪 Ignatius要杀死他们,下面是一些规则。 1.Ignatius只能向四个方向移动(up,down,left,right),每走一步耗时一分,一步是这样定义的,当前坐标为(x,y),走了一步后,Ignatius只能在(x-1,y),(x+1,y),(x,y-1)或(x,y+1)。 2.二维数组被一些字符和数字标记,满足如下定义。 ‘.’:是Ignatius可以走的地方。 ‘X’:是有陷阱的地方,Ignatius不可以走在上面  n  :代表这里有一个怪有n HP,如果Ignatius走在这上面,需要花n分钟杀死这个怪 你的任务是输出Ignatius抵达目标位置的最短时间所对应的路径,开始位置和终止位置永远不可能是陷阱,而且开始的位置一定不会有妖怪。 输入: 多组测试数据,每一组测试数据第一行有两个数,N(2<=N<=100),M(2<=M<=100),代表labyrinth的大小,接下来是一个N*M的二维数组,描述整个labyrinth的情况,输入以文件结尾结束。 输出: 对于每组测试数据,如果英雄到不了目标地方,输出:"God please help our poor hero." ,否则输出:It takes n seconds to reach the target position, let me show you the way."并告诉英雄路径。最后输入FINISH,如果有多条路是时间最短的,那么只用输出一条路即可。
解题思路:
如果这个迷宫中没有妖怪,则就直接按传统的广搜做就可以了,因为后进队的元素对应的时间一定小于等于先进队了,但是由于迷宫中有妖怪,只要勇士走到有妖怪的位置上的时候,就要花费时间将妖怪打死后才能继续前行,因此则后进队的元素对应的时间可能会比先进队的小,因此这个题目要用到的一个知识点就是要使用优先队列,所谓优先队列,首先它是一个队列,但是又有优先二字,也就是说在取队首元素的时候,有一定的选择性,即根据规定的优先级来选择某一项值最优的元素先出队。
对于本题目,指的就是所用时间少的先出队,其次这个题目还有比较特殊的一点就是要记录最短路径,因此和最最传统的广搜题目比较在结构体中应该多加一个字符串类型的变量path,来记录路径。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <string>

using namespace std;

char Map[203][203]; ///迷宫地图
int N,M; ///行,列
int vis[203][203];  ///用来标记的数组。
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};///搜索方向,上、右、下、左
char fangxiang[4]={'u','r','d','l'};
bool flag;  ///用来标记题目有没有解
struct pos
{
    int x; ///横坐标
    int y; ///纵坐标
    int time; ///到达当前位置所用的时间
    string path; ///从起点到达该点的路径。
    friend bool operator < (pos a,pos b)
    {
        return a.time > b.time;
    }
};
void dis_path(pos eend)
{
    if(flag)
    {
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",eend.time);
        int x1=0,y1=0,x2,y2,t=0;
        for(int i = 0; i < eend.path.length();i++)
        {
            if(eend.path[i]=='u') //向上走
            {
                x2=x1+dir[0][0];
                y2=y2+dir[0][1];  //下一位置
                printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
                if(Map[x2][y2]>='1'&&Map[x2][y2]<='9')
                {
                    int n=Map[x2][y2]-'0';
                    while(n--)
                    {
                        printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
                    }
                }
            }
            if(eend.path[i]=='r') //向右走
            {
                x2=x1+dir[1][0];
                y2=y1+dir[1][1];
                printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
                if(Map[x2][y2]>='1'&&Map[x2][y2]<='9')
                {
                    int n=Map[x2][y2]-'0';
                    while(n--)
                    {
                        printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
                    }
                }
            }
            if(eend.path[i]=='d')
            {
                x2=x1+dir[2][0];
                y2=y1+dir[2][1];
                printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
                if(Map[x2][y2]>='1'&&Map[x2][y2]<='9')
                {
                    int n=Map[x2][y2]-'0';
                    while(n--)
                    {
                        printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
                    }
                }
            }
            if(eend.path[i]=='l')
            {
                x2=x1+dir[3][0];
                y2=y1+dir[3][1];
                printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
                if(Map[x2][y2]>='1'&&Map[x2][y2]<='9')
                {
                    int n=Map[x2][y2]-'0';
                    while(n--)
                    {
                        printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
                    }
                }
            }
            x1=x2;
            y1=y2;
        }
        printf("FINISH\n");
    }
}
void BFS()
{
    int i;
    flag = false;
    pos cur,nex;
    cur.x=0;
    cur.y=0;
    cur.time=0;
    cur.path="";
    priority_queue<pos>qu;
    qu.push(cur);
    vis[0][0]=1;
    while(!qu.empty()) ///队列不空
    {
        cur = qu.top();  ///优先队列获取队首元素的方法要用top()
        qu.pop();
        for(i = 0; i < 4; i++)
        {
            nex.x=cur.x+dir[i][0];
            nex.y=cur.y+dir[i][1];
            nex.time = cur.time+1;
            nex.path = cur.path+fangxiang[i];
            if(nex.x>=0&&nex.x<N&&nex.y>=0&&nex.y<M&&vis[nex.x][nex.y]==0&&Map[nex.x][nex.y]!='X')
            {
                /*这一点没有出界,没有被访问过,是可以走的地方*/
                if(nex.x == N-1 && nex.y == M-1) //如果这一点是终点
                {
                    flag = true;
                    /*如果终点位置有妖怪,则要打怪,需要加上打怪的时间*/
                    if(Map[nex.x][nex.y]>='1'&&Map[nex.x][nex.y]<='9')
                        nex.time += Map[nex.x][nex.y]-'0';
                    dis_path(nex);//调用输出路径函数
                    return;
                }
                else //不是终点
                {
                    vis[nex.x][nex.y]=1;  //标记证明已到过这个地方
                    if(Map[nex.x][nex.y]>='1'&&Map[nex.x][nex.y]<='9')
                        nex.time += Map[nex.x][nex.y]-'0';
                    qu.push(nex);
                }
            }
        }
    }
    if(flag == false)
        printf("God please help our poor hero.\nFINISH\n");
    return;
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        int i,j;
        for(i = 0; i < N; i++)
            for(j = 0; j < M; j++)
        {
            scanf(" %c",&Map[i][j]);
            vis[i][j]=0;
        }
        BFS();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值