hdu acm 1548 A strange lift
来源:互联网 发布:怎样申请淘宝直播间 编辑:程序博客网 时间:2024/06/07 04:48
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 53 3 1 2 50
Sample Output
3
#include<iostream>#include<queue>using namespace std;struct node{ int x; int t;}n1,n2,m;int vis[205],h[205],flag;int main(){ int n,a,b,i; while(cin>>n&&n) {flag=0; cin>>a>>b; for(i=1;i<n+1;i++) { cin>>h[i]; vis[i]=0; } n1.x=a;n1.t=0; queue<node>q; q.push(n1); while(!q.empty()) { m=q.front(); q.pop(); if(m.x==b) { flag=1; break; } n1.x=m.x-h[m.x]; n2.x=m.x+h[m.x]; if(n1.x>=0&&n1.x<=b&&!vis[n1.x]) { n1.t=m.t+1; vis[n1.x]=1; q.push(n1); } if(n2.x>=0&&n2.x<=b&&!vis[n2.x]) { n2.t=m.t+1; vis[n2.x]=1; q.push(n2); } } if(flag==1)cout<<m.t<<endl; else cout<<"-1\n"; } return 0;}
0 0
- hdu acm 1548 A strange lift
- A strange lift hdu 1548
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- Hdu-1548 A strange lift
- HDU 1548 A strange lift
- HDU-1548-A strange lift
- hdu 1548 A strange lift
- HDU 1548-A strange lift
- HDU 1548 A strange lift
- hdu 1548 A strange lift
- hdu 1548 A strange lift
- HDU 1548 A strange lift
- hdu 1548 a strange lift
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- hdu 1548 A strange lift
- php数据库编程:利用mysql扩展库操作数据库。
- 判断两个字符串是否为兄弟字符串
- hdu2181C - 哈密顿绕行世界问题
- Cocos2d-JS使用log方法控制台无法打印出结果
- DP-POJ-1458-Common Subsequence
- hdu acm 1548 A strange lift
- ImageNet大规模视觉识别竞赛 ILSVRC 2015 尝鲜数据集
- 软件架构师应该知道的97件事之概括76-90
- Cocos2D-x内存管理的原则
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest K题:King's Rout [topo排序]
- 自动化测试(转自百度百科)
- Java如何在Cmd实现清屏功能
- python日志管理模块logging学习
- 关于线程启动