hdu1548 A strange lift(bfs)

本文介绍了一种使用广度优先搜索(BFS)解决特殊电梯问题的方法,旨在找到从起点到终点所需的最少按钮按压次数。电梯在每层楼有不同的上行和下行楼层数,通过BFS算法有效地寻找解决方案。

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

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22088    Accepted Submission(s): 8070


Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
 

Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
 

Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
 

Sample Input
5 1 5 3 3 1 2 5 0
 

Sample Output
3

题意:有1~n层楼,每层可以上或者下一定的距离,问从a楼到b楼最少需要上下几次电梯。

题解:简单的bfs找最小的次数,bfs搜索的层数就是最小的次数,搜索不到就说明不能到那一层,输出-1。搜索完一层标记一下就行了。

代码:

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int f[1000];
int flag[1000];
int step[1000];
int n;

void bfs(int a, int b)
{
	queue<int > q;
	q.push(a);
	flag[a] = 1;
	int t, next;
	while(!q.empty())
	{
		t = q.front();
		q.pop();
		if(t == b)
			break;
		next = t + f[t];
		if(next > 0 && next <= n && flag[next] == 0)
		{
			q.push(next);
			flag[next] = 1;
			step[next] = step[t] + 1; 
		}
		next = t - f[t];
		if(next > 0 && next <= n && flag[next] == 0)
		{
			q.push(next);
			flag[next] = 1;
			step[next] = step[t] + 1; 
		}
	}
}

int main()
{
	int a, b;
	while(cin >> n && n != 0)
	{
		cin >> a >> b;
		for(int i = 1; i <= n; i++)
		{
			cin >> f[i];
		}
		memset(flag,0,sizeof(flag));
		memset(step, 0, sizeof(step));
		bfs(a, b);
		if(flag[b])
			cout << step[b] << endl;
		else
			cout << "-1" << endl;
	}
	
	return 0;
} 


内容概要:本文档详细介绍了一个基于MATLAB实现的电力负荷预测项目,该项目运用遗传算法(GA)优化支持向量回归(SVR)和支持向量机(SVM)模型的超参数及特征选择。项目旨在解决电力系统调度、发电计划、需求侧响应等多个应用场景中的关键问题,特别是在应对高比例可再生能源接入带来的非线性、非平稳负荷预测挑战。文中涵盖了从数据接入、特征工程、模型训练到部署上线的全流程,包括详细的代码示例和GUI设计,确保方案的可复现性和实用性。 适用人群:具备一定编程基础,尤其是熟悉MATLAB语言和机器学习算法的研发人员;从事电力系统调度、电力市场交易、新能源消纳等相关领域的工程师和技术专家。 使用场景及目标:①通过构建面向小时级别的滚动预测,输出高分辨率负荷轨迹,为日内与日前滚动调度提供边际成本最小化的依据;②在负荷高峰和供给紧张时,通过价格信号或直接负荷控制实施需求侧响应,提升削峰效率并抑制反弹;③为灵活性资源(调峰机组、储能、可中断负荷)提供更清晰的出清路径,降低弃风弃光率,提升系统整体清洁度;④帮助市场主体更准确地评估边际出清价格变化,提高报价成功率与收益稳定性,同时降低由预测偏差带来的风险敞口;⑤在运维与审计场景中,对预测产生的原因进行说明,保障业务侧与监管侧的可追溯性。 阅读建议:此资源不仅提供了完整的代码实现和GUI设计,更注重于理解GA优化过程中涉及到的数据处理、特征构造、模型选择及评估等核心步骤。因此,在学习过程中,建议结合实际案例进行实践,并深入研究每个阶段的具体实现细节,特别是适应度函数的设计、超参数空间的定义以及多样性维护机制的应用。此外,关注项目中关于数据对齐、缺失值处理、特征标准化等方面的最佳实践,有助于提高模型的鲁棒性和泛化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值