A strange lift HDU
来源:互联网 发布:仓库软件多少钱 编辑:程序博客网 时间:2024/05/16 07:32
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"?
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.
5 1 53 3 1 2 50
3
#include<iostream>#include<cstring>#include <algorithm>#include <queue>using namespace std;typedef struct Elevator{int first,second;}P;/*typedef pair<int, int> P;*/const int M = 205;int N, A, B, num = 0;int idx[M];int k[M];int bfs(){ P p, p1; queue<P> q; memset(idx, 0, sizeof(idx)); p.first = A; p.second = 0; idx[p.first] = 1; q.push(p); while(!q.empty()) { p = q.front(); q.pop(); if(p.first == B) return p.second; p1.first = p.first - k[p.first]; if(p1.first > 0 && p1.first <= N && !idx[p1.first]){p1.second = p.second + 1;idx[p.first] = 1;q.push(p1);} p1.first = p.first + k[p.first]; if(p1.first > 0 && p1.first <= N && !idx[p1.first]){p1.second = p.second + 1;idx[p.first] = 1;q.push(p1);} } return -1;}int main(){ while(cin>>N) { if(!N) break; cin>>A>>B; for(int i = 1; i <= N; i++) cin>>k[i]; cout<<bfs()<<endl; } return 0;}
阅读全文
0 0
- HDU A strange lift
- HDU A strange lift
- A strange lift HDU
- A strange lift HDU
- HDU 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
- ubuntu 菜单栏消失
- hdu 1150-匈牙利算法
- 《设计模式之禅》读书笔记-策略模式、享元模式、工厂模式、门面模式混用
- 深入理解JavaScript系列(29):设计模式之装饰者模式
- phpmyadmin的config-default-php文件中各个配置参数的详细说明
- A strange lift HDU
- 图像处理与计算机视觉中的经典文章
- 算法——排序之归并排序
- 关于AppCompatTextView
- 【Redis缓存机制】2.Redis安装和简单使用
- 深入理解JavaScript系列(30):设计模式之外观模式
- android.os.Debug.waitForDebugger()使用需小心
- mysql索引
- hadoop 入门