杭电-1026Ignatius and the Princess I(BFS+记录路径)

从零构建个性化AI助手:基于Mem0与本地大模型的记忆化实践 本文详细介绍了如何利用Mem0与本地大模型构建个性化AI助手,实现记忆化应用结构的实践方法。从开发环境搭建、记忆存储机制到个性化交互实现,提供了完整的解决方案和代码示例,帮助开发者在本地环境中创建能记住用户习惯和偏好的智能助手。 阅读详情

Ignatius and the Princess I

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


这个题看着挺难的,其实也不简单!

主要意思是,从左上角走到右下角,若能走到,记录路径并输出!

这里记录路径我用的是结构体数组+栈

一定注意下,用队列前要清空队列,否则超内存

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int m,n,vis[102][102],mov[4][2]={0,1,0,-1,1,0,-1,0}; 
char map[102][102];
struct node
{
	friend bool operator<(node a,node b)
	{
		return a.step>b.step;
	}
	int x,y,step;
}ta,tb;
struct nodex
{
	int x;
	int y;
}mm;
nodex best[102][102];
priority_queue<node>q;
stack<nodex> w;
bool can(node x)
{
	if(x.x<0||x.x>m-1||x.y<0||x.y>n-1||vis[x.x][x.y]||map[x.x][x.y]=='X')
	return false;
	return true;
}
int bfs()
{
	int i,cot=0;
	ta.x=0;
	ta.y=0;
	ta.step=0;
	while(!q.empty())
	q.pop();//否则超内存 
	vis[ta.x][ta.y] = 1;
	q.push(ta);
	while(!q.empty())
	{
		ta=q.top();
		q.pop();
		if(ta.x==m-1&&ta.y==n-1)
			return ta.step;
		for(i=0;i<4;i++)
		{
			tb.x=ta.x+mov[i][0];
			tb.y=ta.y+mov[i][1];
			if(can(tb))
			{
				mm.x=ta.x;
				mm.y=ta.y;
				if(map[tb.x][tb.y]>='1'&&map[tb.x][tb.y]<='9')
				tb.step=ta.step+map[tb.x][tb.y]-'0'+1;
				else
				tb.step=ta.step+1;
				
				vis[tb.x][tb.y]=1;
				q.push(tb);
				best[tb.x][tb.y]=mm;
			}
		}
	}
	return 0;
}
int main()
{
	int i;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=0;i<m;i++)
		scanf("%s",map[i]);
		memset(vis,0,sizeof(vis));
		int t=bfs();
		if(!t)
		printf("God please help our poor hero.\nFINISH\n");
		else
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",t);
			nodex qq;
			best[0][0].x=-1;
			best[0][0].y=-1; 
			qq.x=m-1;
			qq.y=n-1;
			while(best[qq.x][qq.y].x!=-1&&best[qq.x][qq.y].y!=-1)
			{
				w.push(qq);
				qq=best[qq.x][qq.y];
			}
			int time=1;
			while(!w.empty())
			{
				printf("%ds:(%d,%d)->(%d,%d)\n",time++,qq.x,qq.y,w.top().x,w.top().y);
				qq=w.top();
				if(map[qq.x][qq.y]!='.')
				{
					for(i=1;i<map[qq.x][qq.y]-'0'+1;i++)
					printf("%ds:FIGHT AT (%d,%d)\n",time++,qq.x,qq.y);
				}
				w.pop();
			}
			printf("FINISH\n");
		}
	}
	return 0;
}



下面是我用DFS写的,不过,超时了!


#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
char map[110][110];
int m,n,nex[10100],vis[110][110],mov[4][2]={0,1,0,-1,1,0,-1,0},best,cot; 
int bes[10100];
bool can(int x,int y)
{
	if(x<0||x>m-1||y<0||y>n-1||map[x][y]=='X'||vis[x][y])
	return false;
	return true;
}
void dfs(int x,int y,int time)
{
	int i,j;
	if(x==m-1&&y==n-1)
	{
		if(time<best)
		{
			best=time;
			for(i=0;i<cot;i++)
			bes[i]=nex[i];
		}
		return ;
	}
	for(i=0;i<4;i++)
	{
		int xx=x+mov[i][0],yy=y+mov[i][1];
		if(can(xx,yy))
		{
			vis[xx][yy]=1;
			if(map[xx][yy]>='1'&&map[xx][yy]<='9')
			{
				for(j=0;j<=map[xx][yy]-'0';j++)
				nex[cot++]=xx*n+yy;
				dfs(xx,yy,time+map[xx][yy]-'0'+1);
				cot-=(map[xx][yy]-'0'+1);
			}
			else
			{
				nex[cot]=xx*n+yy;
				cot++;
				dfs(xx,yy,time+1);
				cot--;
			}
			vis[xx][yy]=0;
		}
	}
}
int main()
{
	int i,j;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=0;i<m;i++)
		scanf("%s",map[i]);
		best=INF;
		memset(vis,0,sizeof(vis));
		memset(nex,0,sizeof(nex));
		memset(bes,0,sizeof(bes));
		vis[0][0]=1;
		nex[0]=0;
		cot=1;
		dfs(0,0,0);
		if(best==INF)
		printf("God please help our poor hero.\nFINISH\n");
		else
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",best);	
			for(i=1;i<=best;i++)
			{
				int x1=bes[i-1]/n;
				int y1=bes[i-1]%n;
				int x2=bes[i]/n;
				int y2=bes[i]%n;
				if(bes[i]!=bes[i-1])
				{
					printf("%ds:(%d,%d)->(%d,%d)\n",i,x1,y1,x2,y2);
				}
				else
				{
					printf("%ds:FIGHT AT (%d,%d)\n",i,x2,y2);
				}
			}
			printf("FINISH\n");		
		}
	}
	return 0;
}




解决ADS1.2报错Cannot obtain license 报错如下 将licenses文件下的license内容进行修改: 修改为如下内容即可。 # # Generated on 2021-dec-1 by licwizard # PACKAGE ads armlmd 1.200 E32F0DE5161D COMPONENTS="armasm compiler bats armulate axd adwu fromelf armlink codewarrior armsd" INCREMENT ads armlmd 1.200 permanent uncounte 阅读详情

相关推荐

灯塔问题:贪心与动态规划算法解析

区间覆盖问题是算法设计中的经典案例,常用于理解贪心算法与动态规划的核心差异。贪心算法通过局部最优选择逐步构建全局解,具有O(n log n)的高效时间复杂度,特别适合处理一维区间覆盖场景。动态规划则采用自底向上的填表法,虽然通用性更强但复杂度较高。在实际工程中,这类算法广泛应用于无线基站部署、监控设备布置等资源优化场景。通过分析灯塔问题的两种解法,可以深入掌握算法选择的关键因素,如问题规模、性能要求和实现复杂度。

weixin_28720573的博客 259

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): 17058    Accepted Submission(s): 5459 Special Judge Problem

j6635788的博客 305

QCustomPlot实现毫秒级时间轴数据的动态可视化与实时刷新优化

本文深入探讨了如何利用QCustomPlot库高效实现毫秒级时间轴数据的动态可视化与实时刷新。文章从基础配置讲起,详细介绍了时间轴初始化、数据平滑滚动、增量更新与内存管理等核心优化技巧,并分享了应对高频数据的缓冲策略及OpenGL硬件加速等高级优化手段,旨在解决实时图表卡顿问题,提升流畅度。

weixin_29012677的博客 424

杭电 1026 Ignatius and the Princess I BFS 搜索 JAVA

题意:n*m迷宫,求从(0,0)到(n-1,m-1)的最少时间。'X'是墙,'.'是空地,'1'-'9'表示有怪物,消灭之需要数字对应的时间。 package DFS; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Stack; public class HD1026 { int n,

qq_35109846的博客 353

KTransformers安装笔记 利用docker安装KTransformers

KTransformers安装笔记 利用docker安装KTransformers

guoqingru0311的博客 1560

基于深度学习方法(7种)的高光谱与多光谱图像融合(附代码+3个数据集获取)

其他不懂的可以把代码发给AI哦,让它给你解读。

热爱分享,热爱记录 1298

HDU 1026 (BFS + 优先队列)

http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into

积跬步致千里 801

ADS实战指南:负载牵引(Loadpull)在射频功放设计中的关键应用

本文深入解析了负载牵引(Loadpull)技术在射频功率放大器设计中的核心作用与实战方法。通过生动的比喻和详细的ADS软件操作指南,阐述了如何通过系统扫描负载阻抗,在史密斯圆图上绘制等功率线与等效率线,从而精准找到最优阻抗点,以平衡输出功率与效率,是应对5G等现代通信系统严苛要求的必备技能。

weixin_29198045的博客 601

杭电1026_ Ignatius and the Princess I (BFS+模拟 或者 BFS+优先队列)——java

Problem DescriptionThe 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. T...

一只笨鸟丶的专栏 488

Eplan P8自动线号生成全攻略:从电位定义点到连接编号的完整流程

本文详细解析了Eplan P8自动线号生成的全流程,从电位定义点的创建配置到连接编号的标准化管理,帮助电气设计人员提升图纸规范性和工作效率。重点介绍了自动编号方案的定义与调整技巧,以及企业级标准化实施建议,是掌握Eplan P8自动线号功能的实用指南。

weixin_29254029的博客 510

Autosar诊断实战系列07-Dcm之0x19 06扩展数据配置及工程问题讨论

19 06扩展数据如何配置?如果某DTC没有出现过故障扩展数据该如何响应?

initiallizer的博客 1704

杭电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): 12019    Accepted Submission(s): 3789 Special Judge Problem D

听自己心跳的声音 589

python基础:工作会用到的95%的基础python代码都在这了(万字详解!)

本文是一份Python编程语言的快速参考备忘单,涵盖了Python基础语法和常用数据类型。主要内容包括:Python入门指南(打印、变量声明)、内置数据类型(字符串、数值、列表、元组、集合、字典等)、控制结构(判断、循环)、函数定义、文件处理操作以及字符串操作方法(切片、循环遍历等)。这个备忘单特别适合初学者快速查阅Python基础语法,同时也提供了数据类型转换、f-string格式化字符串等实用技巧。通过简洁的代码示例展示了Python的基本语法规则和典型用法。

数据知道的博客 8160

杭电OJ 1026:Ignatius and the Princess I

比较麻烦的一道题目,自己刚开始思路不清楚,

761

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

hdoj1026 Ignatius and the Princess I(图bfs+栈)

感觉自己还有好长好长的路要走。。 考察:bfs时用到队列找到最优路线,用栈存储经过的点并输出。 #include #include #include using namespace std; typedef struct Node{ int x, y, cost; int prex, prey; }Node; int N, M; char maze[105][105];

CqZtw的博客 593

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU杭电1026 Ignatius and the Princess I(迷宫问题bfs)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意,x代表不能走的路,小点 代表可以走的路,数字代表需要在此处停留的步数(时间)。 BFS广搜即可。但这个题有一个值得关注的地方,他需要在数字处停留几步来打怪,那么有一个问题是:步数最少找到出口的方案,会因为打怪而时间(或者说步数)变长,就有可能不在是最短时间。 我们知道bfs广

雪的期许 1477

OV9734 DATASHEET,完整规格书

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

杭电 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): 15516    Accepted Submission(s): 4911 Special Judge Problem

mengxiang000000 1806

ACM_搜索:杭电oj1026:Ignatius and the Princess I

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:一个人要从N*M矩形地图的左上角走到右下角.且只能向四个方向走.X表示墙不能走.字符1-9表示怪物并且数字代表怪物的血量.也就是杀死怪物需要该数字的单位时间.人每走一个格子花费1个单位的时间.然后要你按照格式打印最短时间的路径和具体的路径. 简单的最短路径题.直接用bfs做.用二...

闭上左眼看世界的博客 601

【搜索之BFS + 优先队列】杭电 hdu 1026 Ignatius and the Princess I

/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2012 panyanyany All rights reserved. URL : http://acm.hdu.edu

panyanyany(飞雨幻沙) 1107

hdu 1026 Ignatius and the Princess I (bfs)

杭电第一版bfs水题,然而还是让坑了

巴达鸟巴达鸟 432

HDUOJ-1026 Ignatius and the Princess I (时间优先队列+广搜)

解题思路 广搜 使用队列来模拟广搜 数组模拟队列 使用1维数组来模拟队列,head为当前队列头,tail-1为当前队列尾部 优先队列 采用接受了 cmp(time1,time2){return time1 时间优先 递归的方式来应对输出 输出好麻烦,原本怕递归爆栈结果用其他方式输出,结果写了40多行来输出。。还失败了。。到现在都没Debug成功。。遂用递归。。我好菜。。。 Acc

zsdostar 432

HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站

别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9803Accepted Submission(s): 292...

weixin_30460489的博客 59

杭电1018-Big Number(大数)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31165    Accepted Submission(s): 14464 Problem Description In ma

Eric_keke的专栏 2126

杭电1242-Rescue(bfs+优先队列||队列)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21824    Accepted Submission(s): 7776 Problem Description Angel was ca

Eric_keke的专栏 1581

杭电1097-A hard puzzle(快速幂)

A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35177    Accepted Submission(s): 12627 Problem Description lcy give

Eric_keke的专栏 1572

杭电1000- A+B Problem

A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 499575    Accepted Submission(s): 158358 Problem Description Calc

Eric_keke的专栏 1485
上一篇: 杭电-1015Safecracker(DFS)
下一篇: 杭电-2579Dating with girls(2)(bfs+三维标记)
Eric_keke
博客等级 码龄12年 37粉丝 221原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值