hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)

单智能体篇:Prompt工程艺术 模型在接收到 Prompt 时会自动扮演“角色”,角色定义的好坏直接影响智能体输出的风格和专业性。角色定义的重要性让智能体具备明确身份,例如“你是一位专业理财顾问”或“你是一个幽默的文案创作助手”。角色限定了模型知识输出的范围,使回答更专业、更贴合预期。实战示例模糊Prompt给我一些投资建议。你可以投资股票或者基金。优化Prompt你是一位具有10年经验的专业理财顾问,请给我针对30岁男性,年收入20万,风险偏中等的投资组合建议。 阅读详情

题目来源:hdu-1026 Ignatius and the Princess I
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14677 Accepted Submission(s): 4653
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秒,当图为’数字‘时表示需要花费数字秒时间停留,故走过此处需花费时间为:map[][]-‘0’+1;若图为’X’,表示不可走。
题目分析:
此题为迷宫搜索求最短路题,并且输出要包括路径,算是典型的搜索题吧。此题我使用的是bfs搜索,首先需建立一个结构体用来存放走到每个位置的坐标及花费的总时间,使用bfs当然就要用优先队列,在此队列中,把花费时间少的放在队首;另外需要建立一个结构体用来存放所走的最短路线;然后在找到最短路径时依次输出路径即可。
AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 110
using namespace std;
char map[MAX][MAX];
int mark[MAX][MAX];
int dx[4]={1,-1,0,0};               //表示x下一步可走的方向 
int dy[4]={0,0,1,-1};               //表示y下一步可走的方向 
struct node{
    int x,y;
    int time;
    friend bool operator < (node a,node b)
    {
        return a.time > b.time;         //优先队列,输出时间短的 
    }
};
struct Edge{
    int px;
    int py;                             //用来存放前驱坐标 
};
node edge[MAX];
Edge pre[MAX][MAX];
int n,m;

void bfs(int sx,int sy)
{
    mark[sx][sy]=1;         //标记走过的坐标 
    node now,next;          //定义两个结构体,表示当前点与下一个点 
    pre[sx][sy].px=-1;      //标记终点 
    now.x=sx;
    now.y=sy;
    now.time=0;
    if(map[sx][sy]!='.')            
        now.time=map[sx][sy]-'0';
    priority_queue<node>q;  //定义优先队列 
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.x==0&&now.y==0)      //若查找到起点,表明查找结束 
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.time);
            int key=1,total=now.time;
            sx=now.x,sy=now.y;
            while(pre[sx][sy].px!=-1)   //不断找前驱坐标,直至找到终点 
            {
                int tx=pre[sx][sy].px;
                int ty=pre[sx][sy].py;
                printf("%ds:(%d,%d)->(%d,%d)\n",key++,sx,sy,tx,ty);
                if(map[tx][ty]!='.')
                    for(int i=0;i<map[tx][ty]-'0';i++)
                        printf("%ds:FIGHT AT (%d,%d)\n",key++,tx,ty);
                sx=tx;
                sy=ty;
            }
            return ;
        }
        for(int i=0;i<4;i++)    //若还未查找完毕,将此点扩展出的所有满足题意的点入队列 
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(next.x<0||next.x>=n)
                continue;
            if(next.y<0||next.y>=m)
                continue;
            if(mark[next.x][next.y])
                continue;
            mark[next.x][next.y]=1;
            if(map[next.x][next.y]=='X')
                continue;
            if(map[next.x][next.y]=='.')
                next.time=now.time+1;
            else
                next.time=now.time+map[next.x][next.y]-'0'+1;
            pre[next.x][next.y].px=now.x;   //此处用来存放路径
            pre[next.x][next.y].py=now.y;
            q.push(next); 
        }
    }
    printf("God please help our poor hero.\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {   
        getchar();
        memset(mark,0,sizeof(mark));        //初始化标记数组 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                scanf("%c",&map[i][j]);     //存入图 
            getchar();
        }
        bfs(n-1,m-1);                       //从后向前查找,则输出的时候从前往后输出即是路径 
        printf("FINISH\n");
    }
    return 0;
}
基于 STM32 的乒乓球发球机控制系统设计与实现 乒乓球发球机是一种能够按照预设参数自动、连续发出乒乓球的训练设备。本系统以 STM32F103C8T6 为主控核心,通过控制实现球速调节、实现落点与旋转方向控制、实现出球节奏控制,并配合 OLED 显示与按键人机交互,实现速度、频率、落点、旋转四大参数的可调发球训练。 阅读详情

相关推荐

别再手动折腾了!PICO Unity SDK 一键配置开发环境保姆级教程(含PDC串流调试)

本文详细介绍了PICO Unity SDK的一键配置功能,帮助开发者快速搭建VR开发环境。通过自动化配置流程,大幅减少手动设置时间,提升开发效率。文章还涵盖了PDC串流调试技巧和常见问题解决方案,是PICO VR开发的实用指南。

weixin_30335575的博客 458

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

题意:给出n*m的地图,求从(0,0)走到(n-1,m-1)最短时间,方格上的数字表示杀怪所要的时间。 思路:优先队列+bfs,输出路径的时候用一个path数组记录,path[i][j]=d表示(i,j)位置是由d方向过来的。

@you! 1073

Camera AF和FF

Camera按对焦功能可以分为FF(Fixed Focus)和AF(Auto Focus),即固定对焦和自动对焦。固定对焦顾名思义就是焦距是固定的,而自动对焦可以通过移动镜头位置来改变焦距从而实现自动对焦。以前摄像的功能机大多是固定对焦,现在的手机都带有自动对焦功能。 FF模组包括镜头(Lens),传感器(Sensor),红外滤色器(IR Filter),电路板(PCB),镜头支撑(Lens Holder),连接器(Connector)。 Fig 1. FF Module AF模组包括FF模组,音圈马达(V

Coppa的博客 1万+

HDU1026 Ignatius and the Princess I BFS

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22883 Accepted Submission(s): 7378 Special Judge Problem Descrip...

DouglasConnor的博客 549

切比雪夫插值

切比雪夫插值是一种基于切比雪夫节点的多项式插值方法,其优势是减少插值误差(特别是龙格现象:表现为高维插值时在边缘处插值误差骤增)。本文对其基本操作进行说明。

平和少年的博客 1651

单片机通识之UART通信协议(如何用汇编实现)

使用汇编实现UART通信

smart_num_1的博客 1001

BFS hdu 1026 输出路径

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9869    Accepted Submission(s): 2942 Special Judge Problem De

风华绝代 811

攻防世界-Reverse-Game

攻防世界,game,逆向

wuh2333的博客 783

Ignatius and the Princess I HDU - 1026 BFS+优先队列+记录路径

求迷宫最短路径长度和路径。由于有些点会停留打怪,需要多增加停留的时间,而不只是加1。由于队列中每一个扩展点的权重不一样,所以不能按平常的BFS解题。这里采用优先队列,权重大也就是耗时耗路径少的点优先出队。题目还有一个难点就是最短路径。由于BFS就是查找最短路径,所以出列的点构成的路径就是最短路径,我们需要记录每一个点的前驱点/后继点。如果从起点开始搜索到终点,那么记录每个点的后继点;如果从终点开始...

Stay Hungry, Stay Foolish. 340

MCU中GPIO口的驱动方式

MCU中GPIO口的驱动方式 导读: MCU芯片中GPIO口的驱动方式大的可分为输出和输入两类,小的分可以分为8种。 一、四种输入方式 ①上拉输入: 上拉输入抗干扰能力强,特别适合接地信号的输入(比如低电平有效的按键信号输入)。其工作原理如图1所示。 I/O 端口与TTL 施密特触发器之前接入了一个30K~50K 欧姆的上拉电阻,当I/O 端口悬空时,此时MCU 也能读到稳定的高电平状态,...

CSDNFrewen的博客 3382

深入理解 HTTP 协议中的 Content-Type

是 HTTP 协议中的一个头部字段(Header),用于指示请求或响应中实体主体(body)的媒体类型(Media Type),也称为MIME 类型(Multipurpose Internet Mail Extensions)。

h a 6 6 6 c k 为 你解决复杂难题! 1008

Ignatius and the Princess I(hdu1026,带权值的bfs)

/* http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9969    A

JHC23的专栏 1228

RK3568开发笔记- (TX/RX) RGMII delayline 功能调试

本文详细介绍了RK3568芯片RGMII接口delayline功能的调试过程。针对千兆以太网通信不稳定问题,通过分析RGMII时序要求,利用RK3568内置的delayline功能补偿信号延迟。调试方法包括扫描delayline窗口、测试中间值配置、优化硬件设计等步骤。文章还总结了常见问题解决方案,如PHY选型建议和自动扫描功能配置。最终实现了千兆速率下丢包率降至0.01%以下的目标,为类似开发场景提供了实用参考。

flypig has a dream! 323

HDU1026——Ignatius and the Princess I,HDU1027——Ignatius and the Princess II,HDU1028——Ignatius and the

a矩阵:存储迷宫的信息,其中X表示障碍物,表示空地,数字1-9表示需要战斗的格子。flag矩阵:标记格子是否已被访问过。d数组:定义了四个移动方向。node结构体:包含坐标y和x,以及到达该格子的步数step。pre数组:用于记录每个格子的前一个格子,以便回溯路径。

u014114223的博客 2102

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

hdu 1026 Ignatius and the Princess I

hdu   1026   Ignatius and the Princess I                 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:经典二维迷宫问题,魔王、公主与勇士的故事,这不是魔塔么???!!! 题目分析:又是要求输出路径的BFS,还打小怪兽,不过不怕,poj3984打印过路径,hdu1242也

slicer的技术交流栈 825

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HDU OJ 1026 Ignatius and the Princess I 【搜索+记录路径】

原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意: 给一个 n * m 的矩阵,X 代表墙 . 代表路,若是数字 ,则该点有 怪,需要 打怪。从求 从(0,0 )点 到  (n-1,m-1)点的用时最小的路,要输出路径。 思路:1:求用时最小路----BFS(广搜)+ 优先队列;             2:在搜到一个点时对应记

nyist_飘谊 2672

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Hud 1026 Ignatius and the Princess I[Bfs]

题目连接:点击打开链接 广搜,要优先队列。 打印路径:要用递归。递归算是自己的弱项了吧。以后要加强学习。 代码:#include #include #include #include using namespace std; const int N=110; char map[N][N]; bool flag[N][N]; int dr[N][N]; int n,m,k,T; int dx

1576

hdu1026 Ignatius and the Princess I (bfs)

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,

just_for_your_smile的专栏 1万+

HDU1026 Ignatius and the Princess I BFS+路径输出

要求最短时间,使用BFS,使用优先队列,将时间少的放到堆顶。先出堆。 然后由于要输出路径,在节点中加入PRE,设计的数据结构不同这里的方法也不一样,可以将矩阵中的点作为节点,那么就有prex,prey,如果像我一样单个节点作为队列中的元素,则要记录节点编号,我的方法有点麻烦,推荐直接记录x,y的方法。   Ignatius and the Princess I Time Limit: 20

1443

hdu1026 Ignatius and the Princess I(bfs+路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:求最短路长度、路径,有打怪操作,同样在路径中输出。 思路:很好的练习题吧,今天(2016/10/24)第二次做,打怪地方的标记初始化放在了结构体中,半天找不出错。好在改好了,1A~。 #include #include #include #include #incl

Iguodala的博客 432

1026:Ignatius and the Princess I

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 方法:bfs+记忆路径 思路:这是我刷题以来见过的最难的一道题,不但要搜索出最短时间,而且还要记录最短时间所走过的路径,而且还要按照那种诡异的方式进行输出。总之,这道题没有一个方面是好处理的。首先,由于是找最短路径,因此用bfs比较合适,另外,由于要输出路径,所以还要开辟一个数组来储存路径。

恒河沙无数 415

HDU 1026 Ignatius and the Princess I 【优先队列+路径输出】

题意:一个人从左上角出发,到达有下角,‘.’是路,‘X’是墙,数字代表怪物,遇到怪物要把它消灭才能通过,消灭怪兽的时间等于其数值。很明显的要用优先队列+BFS,但记录路径有点困难。 路径怎么记录呢?先举一个简单的例子,假设路径只是一个点,我们用a[]表示,其值与下标的关系: 若a[x]=y,代表到达x这个点的上一点的坐标是y。 此题要用一个结构体的数组,每次BFS的时候,如果下一步可以走,便把当前坐标(上一步)记录下来。 输出的时候倒着输出就可以了。

AC_Dreameng 1440

HDU 1026 Ignatius and the Princess I (BFS,路径输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22707    Accepted Submission(s): 7314Special Judge   Problem Desc...

YIU 260

hdu 1026 Ignatius and the Princess I题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 菜鸟第一发。。。 题目大意:给出一张n*m的图,'.'表示道路,'X'表示不能通过的地方,数字n表示怪物的血量(打死需要花费n秒),每走一步需花费一秒,只能往上下左右四个方向走。要求找出从(0, 0)点到(n-1, m-1)点用时最短的路径,并按照样例把路径输出来。 思路:用bfs寻找

pai_pk的博客 334

HDU 1026 Ignatius and the Princess I

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 点击打开链接 题解:bfs题,难再输出路径,先用一个二维结构体数组存前驱,然后反推得到路径。 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/

GrEedWish_X的专栏 472

【记忆化搜索】HDU 1026 Ignatius and the Princess I

优先队列,记忆化搜索,路径输出。

DT_Zhangshuo的博客 756
上一篇: poj 2506 Tiling(大数+规律)
下一篇: hdu 1874 畅通工程续(Floyd算法)
暮光乐鱼
博客等级 码龄11年 43粉丝 149原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值