HDU 1548【A strange lift】
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31047 Accepted Submission(s): 11151
Total Submission(s): 31047 Accepted Submission(s): 11151
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"?
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.
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
题解:从当前位置开始每次都向上下两个方向扩展,知道走到目标位置为止。DFS也可得出答案,但是会超时,可能是水平不够吧,这里就不贴出来了。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #define INF 99999999 using namespace std; const int maxn=205; int a[maxn]; int vis[maxn]; int minx=INF; int dir[2][1]={-1,1};//上下两个方向 int n,sx,ex; typedef struct Node{ int x; int step=0; }node; void bfs(int x){ node p,t; queue<node> q; p.x=x; p.step=0; q.push(p); while(!q.empty()){ p=q.front(); q.pop(); if(p.x==ex){ if(p.step<minx)//走到目标位置之后还要判断是否当前路径是最短距离 minx=p.step; } for(int i=0;i<2;i++){//向上下两个方向扩展 t.x=p.x+a[p.x]*dir[i][0]; t.step=p.step+1; if(!vis[t.x]&&t.x>0&&t.x<=n){//判断是否已经到过或者是否越界 vis[t.x]=1; q.push(t); } } } return ; } int main() { while(scanf("%d",&n)&&n){ minx=INF; scanf("%d %d",&sx,&ex); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } memset(vis,0,sizeof vis); vis[sx]=1; bfs(sx); if(minx==INF) printf("-1\n"); else printf("%d\n",minx); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用