HDU-1026

本文深入探讨了一个复杂的迷宫寻径问题,通过特定的算法找到从起点到目标点的最短时间路径。介绍了如何利用优先队列优化搜索过程,确保在遇到怪物或陷阱时能够计算出总时间成本。此外,详细讲解了路径输出的实现方法,为读者提供了完整的代码示例。

摘要生成于 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): 23315 Accepted Submission(s): 7563
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

Author
Ignatius.L

又是一道搜索+输出路径。着实写了很久,也参考了网上的代码。。。
这道题,他要的是最短的时间,而按照普通bfs搜到的是最短的路径,所以自己一开始还一直在奇怪为什么路径一直不对,后来看了别人的题解才发现这一点,了解到需要使用优先队列,使时间的优先级较高,这一部分内容还是要学习啊。

感觉这个题一个较难的点就是路径的输出了,调了半天。。。

#include<iostream>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=110;
struct node{
    int x;
    int y;
    int time;
    friend bool operator < (node a,node b)//设置优先级,使时间的优先级高,所以时间短的会排在队首 
    {                                     //看的还不是很懂,需要再翻翻书 
        return a.time>b.time; 
    }
}S,Node,ans[maxn][maxn];
int track[maxn][2];
int X[4]={-1,1,0,0};
int Y[4]={0,0,-1,1};
int m,n;
char G[maxn][maxn];
bool inq[maxn][maxn];
bool test(int x,int y){                                //判断该点是否可走 
    if(x>=0&&x<n&&y>=0&&y<m&&inq[x][y]&&G[x][y]!='X')
    return true;
    else
    return false;
}
int bfs(){
    priority_queue<node>q;
    S.x=0;
    S.y=0;
    if(G[0][0]!='.')                    //起点有怪物的情况考虑 
    S.time=S.time+1+(G[0][0]-'0');
    inq[0][0]=false;
    q.push(S);
    while(q.size())
    {
        node top=q.top();
        q.pop();
        if(top.x==n-1&&top.y==m-1)
        {
            return top.time;
        }
        for(int i=0;i<4;i++)
        {
            int newx=top.x+X[i];
            int newy=top.y+Y[i];
            if(test(newx,newy))
            {
                Node.x=newx;
                Node.y=newy;
                ans[newx][newy]=top;           //用来存储路径 
                if(G[newx][newy]=='.')
                {
                    Node.time=top.time+1;
                }
                else
                Node.time=top.time+1+(G[newx][newy]-'0');
                inq[newx][newy]=false;
                q.push(Node);
            }
        }
    }
    return -1;
}
stack<node> st;
void dfs(int x,int y){            // 深搜来把路上经过的点依次存到栈中 
    if(x==0&&y==0)
    return ;
    st.push(ans[x][y]);
    dfs(ans[x][y].x,ans[x][y].y);
}
int main()
{
    while(cin>>n>>m)
    {
        memset(inq,true,sizeof(inq));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            cin>>G[i][j];
        }
        int cnt=bfs();
        if(cnt==-1)
        {
            cout<<"God please help our poor hero."<<endl<<"FINISH"<<endl;
            continue;
        }
        dfs(n-1,m-1);
        int num=st.size();
        for(int i=1;i<=num;i++)            //依次记录把路径存到数组中 
        {
            track[i][0]=st.top().x;
            track[i][1]=st.top().y;
            st.pop();
        }
        track[num+1][0]=n-1;               //终点没在栈中,存进去 
        track[num+1][1]=m-1;
        cout<<"It takes "<<cnt<<" seconds to reach the target position, let me show you the way."<<endl;
        for(int i=1,k=1;i<=cnt;i++,k++)                //输出路径,i来控制时间,即第几秒 
        {                                              //k来控制路径的输出,因为时间与路径一一并非对应,一个变量不能控制 
            if(G[track[k][0]][track[k][1]]!='.')
            {
                for(int j=i;j<i+(G[track[k][0]][track[k][1]]-'0');j++)
                {
                    cout<<j<<"s:FIGHT AT ("<<track[k][0]<<","<<track[k][1]<<")"<<endl;
                }
                i=i+(G[track[k][0]][track[k][1]]-'0');
                if(i>=cnt)                  //这个大于等于有点玄学...按照一开始我的想法应该是等于,但是wa了,改成大于等于就ac了 
                break;
            }
            cout<<i<<"s:("<<track[k][0]<<","<<track[k][1]<<")->("<<track[k+1][0]<<","<<track[k+1][1]<<")"<<endl;
        }
        cout<<"FINISH"<<endl;
    }
    return 0;
}
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值