(HDU - 1548)A strange lift

A strange lift

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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

题目大意:一个电梯,每层楼都有一个按钮选择上楼还是下楼,当电梯在i层楼可以向上或向下走k[i]层到达i-k[i]或i+k[i]层楼,当然电梯所在楼层不能低于1也不能高于n。

思路:最短路或者bfs即可求解,因为我刚学bfs,所以用bfs进行求解,题目不难,属于基础的bfs,注意输入输出格式即可。

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

const int maxn=205;
bool vis[maxn];
int k[maxn][2];
int n,a,b;

struct node
{
    int x,step;
};

bool check(int x)
{
    return (x>=1&&x<=n);
}

int bfs()
{
    queue<node> q;
    node start,tmp,next;
    start.x=a;
    start.step=0;
    vis[a]=true;
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.x==b) return tmp.step;
        for(int i=0;i<2;i++)
        {
            next.x=tmp.x+k[tmp.x][i];
            if(next.x==b) return tmp.step+1;
            if(check(next.x)&&!vis[next.x])
            {
                next.step=tmp.step+1;
                vis[next.x]=true;
                q.push(next);
            }
        }
    }
    return -1;//不能从a到达b返回-1 
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d%d",&a,&b);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k[i][0]);
            k[i][1]=-k[i][0];
        }
        memset(vis,false,sizeof(vis));
        printf("%d\n",bfs());
    }
    return 0;
}
C语言-光伏MPPT算法:电导增量法扰动观察法+自动全局搜索Plecs最大功率跟踪算法仿真内容概要:本文档主要介绍了一种基于C语言实现的光伏最大功率点跟踪(MPPT)算法,结合电导增量法与扰动观察法,并引入自动全局搜索策略,利用Plecs仿真工具对算法进行建模与仿真验证。文档重点阐述了两种经典MPPT算法的原理、优缺点及其在不同光照和温度条件下的动态响应特性,同时提出一种改进的复合控制策略以提升系统在复杂环境下的跟踪精度与稳定性。通过仿真结果对比分析,验证了所提方法在快速性和准确性方面的优势,适用于光伏发电系统的高效能量转换控制。; 适合人群:具备一定C语言编程基础和电力电子知识背景,从事光伏系统开发、嵌入式控制或新能源技术研发的工程师及高校研究人员;工作年限1-3年的初级至中级研发人员尤为适合。; 使用场景及目标:①掌握电导增量法与扰动观察法在实际光伏系统中的实现机制与切换逻辑;②学习如何在Plecs中搭建MPPT控制系统仿真模型;③实现自动全局搜索以避免传统算法陷入局部峰值问题,提升复杂工况下的最大功率追踪效率;④为光伏逆变器或太阳能充电控制器的算法开发提供技术参考与实现范例。; 阅读建议:建议读者结合文中提供的C语言算法逻辑与Plecs仿真模型同步学习,重点关注算法判断条件、步长调节策略及仿真参数设置。在理解基本原理的基础上,可通过修改光照强度、温度变化曲线等外部扰动因素,进一步测试算法鲁棒性,并尝试将其移植到实际嵌入式平台进行实验验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值