HDU 1026 Ignatius and the Princess I(BFS+优先队列+递归输出路径)

本文介绍了一个迷宫寻径问题,主角Ignatius需在限定时间内找到公主并避开陷阱,同时击败怪物。使用BFS算法进行路径寻找,考虑了打怪时间和路径最优化。文章详细解释了算法实现过程及代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25826 Accepted Submission(s): 7955
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

思路
1.最短时间:BFS
2.会打怪:每打一次,hp -1该节点重新进入队列,利用 优先队列,每一步bfs用时最短的节点排在队列的最前面!

完整代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#define N 111
#define M 111
using namespace std;


char maze[N][M];
char mazee[N][M];
bool vis[N][M];
int way[N][M];
int n,m;
int tt,mn;

int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

struct node{
    int x,y;
    int time;
    friend bool operator < (node a,node b){
        return a.time>b.time;//注意是>,可以自己单独拎出来试一试
    }
};

void bfs(){
    mn=-1;
    priority_queue<node> q;
    node S;
    S.x=0;S.y=0;S.time=0;
    q.push(S);
    vis[0][0]=true;
    while(!q.empty()){
        node top=q.top();
        q.pop();
		
		//hp-1,重新进入队列
        if(maze[top.x][top.y]!='.'){
            top.time++;
            maze[top.x][top.y]--;
            if(maze[top.x][top.y]=='0')//特殊处理
                 maze[top.x][top.y]='.';
            q.push(top);
            continue;
        }
		//成功
        if(top.x==n-1&&top.y==m-1){
            mn=top.time;
            break;
        }

        for(int i=0;i<4;i++){
            node tmp;
            tmp.x=top.x+X[i];
            tmp.y=top.y+Y[i];
            //判断是否能走
            if(!vis[tmp.x][tmp.y]&&tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m&&maze[tmp.x][tmp.y]!='X'){
                tmp.time=top.time+1;
                q.push(tmp);
                vis[tmp.x][tmp.y]=true;//标记
                way[tmp.x][tmp.y]=i;//记录此节点走的方向
            }
        }
    }
}

void print(int x,int y){
    if(x==0&&y==0)//是从终点开始往回推(因为能到终点的只有一条路,这样能唯一确定路径)
        return;
    int nx=x-X[way[x][y]];
    int ny=y-Y[way[x][y]];
    print(nx,ny);//递归
        printf("%ds:(%d,%d)->(%d,%d)\n",tt++,nx,ny,x,y);//输出方向
	
    if(mazee[x][y]!='.'){//如果有怪,打怪(注意这是备用地图,原地图的怪已经死被应用的王子干死了!)
        int blood=mazee[x][y]-'0';
        while(blood--){
            printf("%ds:FIGHT AT (%d,%d)\n",tt++,x,y);

        }
    }

}

int main()
{
    while(cin>>n>>m){
        memset(vis,false,sizeof(vis));
        memset(way,0,sizeof(way));
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            cin>>maze[i][j];
            mazee[i][j]=maze[i][j];
        }
        tt=1;
        bfs();
        if(mn==-1){
            cout<<"God please help our poor hero."<<endl<<"FINISH"<<endl;
        }
        else{
            cout<<"It takes "<<mn<<" seconds to reach the target position, let me show you the way."<<endl;
            print(n-1,m-1);
            cout<<"FINISH"<<endl;
        }

    }
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值