【HDU】 1548 A strange lift
来源:互联网 发布:手机淘宝名字好听 编辑:程序博客网 时间:2024/05/22 15:19
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17322 Accepted Submission(s): 6504
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
题解:这个题直接广搜就可以了,数据又小....0ms过。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int q[205],h,t,vis[205],d[205],a[205],s,b,n;int BFS(int s){ memset(vis,0,sizeof(vis)); h=0; t=0; memset(q,0,sizeof(q)); memset(d,0,sizeof(d)); q[h++]=s; vis[s]=1; while (h!=t) { if (q[t]==b) return d[t]; int up=a[q[t]]+q[t],down=q[t]-a[q[t]]; if (up<=n && !vis[up]) { q[h]=up; d[h]=d[t]+1; vis[up]=1; h++; } if (down>=1 && !vis[down]) { q[h]=down; d[h]=d[t]+1; vis[down]=1; h++; } t++; } return -1;}int main(){ while (scanf("%d%d%d",&n,&s,&b),n) { for (int i=1;i<=n;i++) scanf("%d",&a[i]); printf("%d\n",BFS(s)); } return 0;}
0 0
- 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
- hdu 1548 A strange lift
- “新老三论”浅析
- 正则基础之 \b 单词边界(http://www.jb51.net/article/19330.htm)
- 【JQuery】知识点总结(上)
- 华为机试——字符串排序
- php异步调用客服消息,总是重复三条
- 【HDU】 1548 A strange lift
- Maven 仓库
- 华为机试——查找兄弟单词
- Struts2属性驱动封装(了),需要使用ognl表达式
- CSS中@import的问题
- 华为机试——字符串加解密
- 如何更改应用程序加载基地址
- ROS学习笔记(1):在ROS中使用OpenCV进行简单的图象处理--原理篇
- WIN10下配置java jdk