A strange lift hdu 1548
来源:互联网 发布:易吧进销存软件下载 编辑:程序博客网 时间:2024/06/14 07:11
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8801 Accepted Submission(s): 3327
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
Recommend
8600
果断用bfs, 因为要找的是最短的嘛。
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;char map[300];int maze[300];int n;struct node{ int x; int step;};int check(int x){ if((x>=1)&&(x<=n)) return 1; return 0;}queue<node> q;int bfs(int st,int en){ node fis,n1,n2; int i; if(st==en) return 0; fis.x=st; fis.step=0; q.push(fis); maze[st]=1; while(!q.empty()) { fis=q.front(); q.pop(); if(fis.x==en) { return fis.step; } n1.x=fis.x+map[fis.x]; n2.x=fis.x-map[fis.x]; if(check(n1.x)&&(maze[n1.x]==0)) { n1.step=fis.step+1; maze[n1.x]=1; q.push(n1); } if(check(n2.x)&&(maze[n2.x]==0)) { n2.step=fis.step+1; maze[n2.x]=1; q.push(n2); } } return -1;}int main(){ int i; int st,en; while(scanf("%d",&n)!=EOF) { if(n==0) break; scanf("%d%d",&st,&en); for(i=1;i<=n;i++) { scanf("%d",&map[i]); } memset(maze,0,sizeof(maze)); while(!q.empty()) q.pop(); int tem=bfs(st,en); if(tem==-1) { printf("-1\n"); } else { printf("%d\n",tem); } } return 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
- 函数指针--Nginx和Redis中两种回调函数写法
- 虚拟光驱
- hdu 4652 经典概率题+公式推导
- 深入浅出Node.js(一):什么是Node.js
- socket:bind: Address already in use 端口立即重用
- A strange lift hdu 1548
- SQL2008 无法连接服务器
- Node.js + MySQL
- 关于malloc与字符指针的一些易错点
- Composite 组合(结果型模式)笔记
- 双系统实现文件共享
- hdu 1018(求大数位数)
- hdu 4446
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast