HDOJ 1548 A strange lift (BFS)
来源:互联网 发布:三国志15单机还是网络 编辑:程序博客网 时间:2024/04/30 07:17
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17225 Accepted Submission(s): 6466
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
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;int n,s,e;int ss[205],vis[205];struct node{ int x,step;};int check(int x){ if(x<=0 || x>n) return 1; return 0;}int BFS(){ queue<node> Q; node a,next; int i; a.x = s; a.step = 0; vis[s] = 1; Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(a.x == e) return a.step; for(i = -1;i<=1;i+=2) { next = a; next.x +=i*ss[next.x]; if(check(next.x) || vis[next.x]) continue; vis[next.x] = 1; next.step++; Q.push(next); } } return -1;}int main(){ int i,j; while(~scanf("%d",&n),n) { scanf("%d%d",&s,&e); for(i = 1;i<=n;i++) scanf("%d",&ss[i]); memset(vis,0,sizeof(vis)); printf("%d\n",BFS()); } return 0;}
0 0
- HDOJ 1548 A strange lift (bfs)
- HDOJ 1548 A strange lift (BFS)
- hdoj 1548 A strange lift(bfs)
- HDOJ 1548 A strange lift (BFS)
- hdoj 1548 A strange lift (BFS)
- (BFS)A strange lift--HDOJ
- hdu 1548 A strange lift (bfs)
- HDU 1548 A strange lift(BFS)
- HDU 1548 A strange lift(bfs)
- hdu 1548 A strange lift(bfs)
- hdu 1548 A strange lift(BFS)
- hdu 1548 A strange lift (BFS)
- hdu 1548 A strange lift(BFS)
- POJ 1548 A strange lift(BFS)
- HDU 1548 A strange lift(bfs)
- HDU 1548:A strange lift(BFS)
- hdu-1548-A strange lift(bfs)
- HDU/HDOJ 1548 A strange lift BFS,DFS
- opencv 人脸识别 第二篇
- 可展开的列表组件ExpandableListView
- 解决Android 5.0版本之后出现的异常:Service Intent must be explicit
- 指针笔记
- Activity打开退出动画:从底部向上打开,退出时从上至下退出,避免Activity退出时屏幕短暂黑屏
- HDOJ 1548 A strange lift (BFS)
- ajax中return取不到值的问题
- Java 7之基础 - 强引用、弱引用、软引用、虚引用
- Java多线程编程相关资料推荐
- 圆面积
- 原稿-第10章:聆听数字的声音
- Mybatis3源码分析(05)-加载Configuration-加载MappedStatement
- SearchView搜索框
- HDOJ 2102 A计划 (BFS)