A strange lift HDU
来源:互联网 发布:windows系统模拟器 编辑:程序博客网 时间:2024/05/16 11:25
题目链接:点我
here 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"?
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.
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
题意:
你在一个电梯,每次只能上或者下ki层,问你是否能达到目标层
思路:
bfs + 标记你每次走过的楼层,简单题
代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;int a[210];int A,B,n;bool vis[220];struct ss{ int step,floor;};void bfs(){ memset(vis,false,sizeof(vis)); if(A==B) { printf("0\n"); return ; } ss q[220]; int head=0; int tail=0; q[tail++].floor=A; q[tail-1].step=0; vis[A]=true; while(head<tail){ ss p=q[head++]; int k=p.floor-a[p.floor]; if(k==B){ printf("%d\n",p.step+1); return ; } if(k>0&&k<=n&&vis[k]==false){ q[tail++].floor=k; vis[k]=true; q[tail-1].step=p.step+1; } k=p.floor+a[p.floor]; if(k==B){ printf("%d\n",p.step+1); return ; } if(k>0&&k<=n&&vis[k]==false){ q[tail++].floor=k; vis[k]=true; q[tail-1].step=p.step+1; } } printf("-1\n");}int main(){ while(scanf("%d",&n)!=EOF&&n){ scanf("%d %d",&A,&B); for(int i=1;i<=n;++i) scanf("%d",&a[i]); bfs(); } 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
- 聚合工程maven报错:Could not resolve dependencies for project
- 关于数据库中除法的一个小问题
- 培养属立足于未来的产品经理人1
- USACO Dynamic Programming (1)
- Codeforces 818B Permutation Game
- A strange lift HDU
- hibernate注解主键生成策略
- 浅谈mysql数据库优化技术
- Codeforces第一次rated比赛
- 【spring】Java Web 通过浏览器访问WEB-INF下的静态文件
- IOS
- Java远程调试原理与运用
- Hibernate——关联关系的CRUD(增删改查)二
- Codeforces Round #422 (Div. 2) D题要补的知识点