博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HUD-1548
阅读量:5146 次
发布时间:2019-06-13

本文共 2555 字,大约阅读时间需要 8 分钟。

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 24630    Accepted Submission(s): 8898

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"?
 

 

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 5
3 3 1 2 5
0
 

 

Sample Output
3

 

BFS

 

附AC代码:

1 #include
2 using namespace std; 3 4 int n,s,e; 5 int ss[210],vis[210]; 6 7 struct node{ 8 int x,step; 9 };10 11 int bfs(){12 queue
Q;13 node a,next;14 a.x=s;15 a.step=0;16 vis[s]=1;17 Q.push(a);18 while(!Q.empty()){19 a=Q.front();20 Q.pop();21 if(a.x==e)22 return a.step;23 for(int i=-1;i<=1;i+=2){24 next=a;25 next.x+=i*ss[next.x];26 if(next.x<=0||next.x>n||vis[next.x])27 continue;28 vis[next.x]=1;29 next.step++;30 Q.push(next);31 }32 }33 return -1;34 } 35 36 int main(){37 while(cin>>n&&n){38 cin>>s>>e;39 for(int i=1;i<=n;i++){40 cin>>ss[i];41 }42 memset(vis,0,sizeof(vis));43 cout<
<

 

转载于:https://www.cnblogs.com/Kiven5197/p/6641973.html

你可能感兴趣的文章
使用xcopy命令,从服务器copy最新文件到本地,实现程序版本更新
查看>>
java 8 新特性
查看>>
LIS O(n^2)模板
查看>>
怎样办理DB2中呈现的SQL1032N错误现象
查看>>
ParolaPass:暗码天生器
查看>>
Fireflix:便利 Flickr 用户的 Firefox 扩展
查看>>
ubuntu下装配db2 express
查看>>
Informix IDS 11系统操持(918考试)认证指南,第8部门:面向操持员的SQL特性(4)
查看>>
SpringBoot,SSM和SSH
查看>>
微信开发如何做本地调试?
查看>>
蓝鲸 修改主机名重装后初始化不了cmdb安装不了job + 数据采集流程
查看>>
multiprocessing 源码解析 更新中......
查看>>
redis List命令,php操作Redis List函数介绍
查看>>
python opencv
查看>>
git
查看>>
饮冰三年-人工智能-linux-08 软件包管理(Python的安装)
查看>>
DOS命令大全
查看>>
HDU 2512 一卡通大冒险(第二类斯特林数+贝尔数)
查看>>
Codeforces Round #104 (Div. 1)
查看>>
重载运算符
查看>>