hdu 1548 最短路
来源:互联网 发布:宜家值得买 知乎 编辑:程序博客网 时间:2024/06/01 09:41
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7604 Accepted Submission(s): 2856
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<iostream>#include<stdio.h>#include<string.h>#include<queue>using namespace std;int n,a,b;int step[205];int floor[205];bool visit[205];int minn;int bfs(int i){ memset(step,0,sizeof(step)); step[i]=0; queue<int> q; q.push(i); while(!q.empty()) { int u=q.front(); q.pop(); if(u==b) { return step[u]; } if(u+floor[u]<=n&&!visit[floor[u]+u]) { visit[floor[u]+u]=true; step[u+floor[u]]=step[u]+1; q.push(u+floor[u]); } if(u-floor[u]>0&&!visit[u-floor[u]]) { visit[u-floor[u]]=true; step[u-floor[u]]=step[u]+1; q.push(u-floor[u]); } } return -1;}int main(){ while(scanf("%d",&n),n) { scanf("%d%d",&a,&b); memset(visit,false,sizeof(visit)); for(int i=1;i<=n;i++) { scanf("%d",&floor[i]); } if(a<=0||a>n||b<=0||b>n) { printf("-1\n"); continue; } visit[a]=true; int ans=bfs(a); if(ans!=-1) { printf("%d\n",ans); } else printf("-1\n"); } return 0;}
- hdu 1548 最短路
- hdu 1548 最短路
- hdu 1548 最短路
- hdu 1548(最短路)
- hdu 1548 最短路||bfs
- hdu 1548 bfs 或 单向最短路
- hdu 1548 A strange lift(最短路)
- HDU 1548 最短路---A strange life
- 最短路 2544hdu
- HDU 2544 最短路
- hdu 1245 最短路
- HDU最短路总结
- hDU 最短路
- hdu 1874 (最短路)
- hdu 2112 (最短路)
- HDU-2544 最短路
- Hdu-2544 最短路
- hdu 2066 最短路
- 浙江大华2011.10.10校园招聘会笔试题
- android下SQLite学习笔记
- 冒泡排序、插入排序、选择排序--C语言实现
- Bash游戏 V3
- 数据结构复习笔记(一)
- hdu 1548 最短路
- 个性化设置 MySQL prompt 提示符
- A* (路径搜索)算法导引
- 软件测试DAY02
- MySql存储过程—1、SQL存储过程的基础知识
- 单点登录技术Cookie问题
- nc另类应用:内网linux机器A(定时)访问家里无线路由下的机器B
- html5视频播放
- DBCP数据源下载和配置