HDU 1548 A strange lift
来源:互联网 发布:php json 中括号 编辑:程序博客网 时间:2024/06/07 07:08
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<cstdio>#include<cstring>#include<queue>using namespace std;int N,A,B,Next[210],dist[210];void readdata(){scanf("%d%d",&A,&B);for(int i=1;i<=N;i++)scanf("%d",&Next[i]);}int bfs(){memset(dist,0,sizeof(dist));queue<int> q;q.push(A); dist[A]=1;while(!q.empty()){int k=q.front(); q.pop();if(k==B) return dist[B]-1;if(k+Next[k]<=N && !dist[k+Next[k]]){dist[k+Next[k]]=dist[k]+1;q.push(k+Next[k]);}if(k-Next[k]>=0 && !dist[k-Next[k]]){dist[k-Next[k]]=dist[k]+1;q.push(k-Next[k]);}}return -1;}int main(){while(scanf("%d",&N)==1){if(N==0) break;readdata();printf("%d\n",bfs());}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
- [Python02]-Python集合的使用
- 9.6 Git 内部原理 - 传输协议
- T4模板基础
- xapic , x2apic
- java浮点数比较
- HDU 1548 A strange lift
- 9.7 Git 内部原理 - 维护及数据恢复
- Android菜单简析02(ContextMenu)
- 1.信号处理之:kill(),alarm(),pause()函数
- 图解SQL多表关联查询
- cf Two Sets (我用二分最大匹配做的)
- LeetCode -- Median of Two Sorted Arrays
- 一阶二阶电路滤波器的软件方式实现
- JAVA输入输出总结