HDU 1548
2015-07-25 16:13
417 查看
A strange lift
[align=center]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15642 Accepted Submission(s): 5853
[/align]
[align=left]Problem Description[/align]
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"?
[align=left] [/align]
[align=left]
[/align]
[align=left]Input[/align]
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.
[align=left] [/align]
[align=left]
[/align]
[align=left]Output[/align]
[align=left]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".[/align]
[align=left]Sample Input[/align]
5 1 5
3 3 1 2 5
0
[align=left]Sample Output[/align]
3
题意是通过既定的规则从某一层到达另一层,这道题的特点在于每一层都有自己的步数,所以在广度搜索时要记得对应层数,并且限定范围防止超出输入的层数界限,我预先将所有的位置步数初始化成-1所以不注意输入的层数限制会出问题(没注意这点当成越界问题结果WA了),如果使用其他方式来记录就可以无视这点,一旦目标层数有了步数记录,就可以结束搜索了。
#include<iostream> #include<cstdio> #include<queue> #include<memory.h> using namespace std; int N,K,L,D; int d[500]; int a[410]; queue<int> M; void bfs(int n){ while(!M.empty()) M.pop(); M.push(n); while(!M.empty()&&a[K]==-1){ int nx1,nx2,cx; cx=M.front(); M.pop(); D=a[cx]+1; nx1=cx+d[cx]; nx2=cx-d[cx]; if(nx1>0&&nx1<=K&&a[nx1]==-1){ M.push(nx1); a[nx1]=D; } if(nx2>0&&nx2<=K&&a[nx2]==-1){ M.push(nx2); a[nx2]=D; } } } int main() { while(scanf("%d",&L)!=EOF){ if(L==0) break; scanf("%d%d",&N,&K); memset(a,-1,sizeof(a)); a =0; for(int i=1;i<=L;i++) scanf("%d",&d[i]); if(N==K){ printf("0\n"); }else{ bfs(N); printf("%d\n",a[K]); } } return 0; }
相关文章推荐
- DOM简述
- C++生成二维码总结
- EditView软键盘管理
- 李航博士:浅谈我对机器学习的理解
- Angular2.0-组件
- Matlab中fread函数的高级使用方法及其帮助文档中容易引起歧义的地方
- 求一个整形数组的和最大的连续子串 -------- 程序员面试金典
- chromium:LoadUrl之后会发生那些事情之CompositedLayer 创建
- 九零后的五年七次工作经历
- Codeforces Round #313 (Div. 2)E. Gerald and Giant Chess(Lucas定理+dp)
- 强联通图的Tarjan算法
- hdu3530 Subsequence 单调队列
- Android中音量的控制
- Mybatis中配置Mapper的方法
- 使用python 处理发送POST请求
- Oracle常见问题,及处理。
- Android中的进程和线程
- [C++11] thread
- ubuntu设置系统时间与网络时间同步(转)
- HashCode和equal方法