hdu 1548 A strange lift
来源:互联网 发布:什么是淘宝客单价 编辑:程序博客网 时间:2024/06/04 19:44
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直接一个队列 广搜,没什么说的。
还有就是,Dijkstra最短路也能写;
我用广搜写的。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct Node{ int x,f,step;}fl[205],s,t,up,down;int n,a,b;int bfs(){ bool v[205]; memset(v,false,sizeof(v)); queue<Node>que; que.push(s); v[s.f]=true; while(!que.empty()){ t=que.front();//printf("%d\n",t.f); que.pop(); up=down=t; up.f=t.f+t.x; down.f=t.f-t.x; if(up.f<=n&&!v[up.f]){ up.x=fl[up.f].x; up.step++; v[up.f]=true; que.push(up); } if(down.f>0&&!v[down.f]){ down.x=fl[down.f].x; down.step++; v[down.f]=true; que.push(down); } if(t.f==b) return t.step; } return -1;}int main(){ while(~scanf("%d",&n),n){ scanf("%d%d",&a,&b); for(int i=1;i<=n;i++){ scanf("%d",&fl[i].x); fl[i].step=1; fl[i].f=i; if(i==a){ s.x=fl[i].x; s.step=0; s.f=i; } } 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
- 数组、指针数组与数组指针
- 设计模式——状态模式
- js中的delete运算符
- 布局出问题?边框大法好!
- IOS 客户端 socket IPV4兼容IPV6
- hdu 1548 A strange lift
- Spring入门
- OSGI中ServiceTracker的应用
- 红与黑HNCU1103
- 9. PHP 绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数
- unity3d接入支付宝 iOS篇(二)
- SQLServer控制用户访问权限表
- iOS 继承&类方法
- 数据库SQL优化大总结之 百万级数据库优化方案