A strange lift HDU - 1548
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.OutputFor 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 Code:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int INF=0x3f3f3f3f;
int N,A,B;
int a[];
int vis[];
int bfs(){
if(A>N||B>N) return -;
queue<int> q;
q.push(A);
vis[A]=;
while(!q.empty() ){
int p=q.front() ;
q.pop() ;
if(p==B) return vis[p];
int up,down;
up=p+a[p];
down=p-a[p];
if(up<=N&&up>=&&vis[up]==INF){
vis[up]=vis[p] +;
q.push(up);
}
if(down>=&&down<=N&&vis[down]==INF){
vis[down]=vis[p]+;
q.push(down);
}
}
return -;
}
int main(){
while(cin>>N>>A>>B&&A){
for(int i=;i<=N;i++) cin>>a[i];
memset(vis,INF,sizeof(vis));
cout<<bfs()<<endl;
}
}
A strange lift HDU - 1548的更多相关文章
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
随机推荐
- Flyway Overview and Installation
https://flywaydb.org/documentation/ Flyway is an open-source database migration tool. It strongly fa ...
- 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos
End-to-End Learning of Action Detection from Frame Glimpses in Videos CVPR 2016 Motivation: 本 ...
- [CodeForces - 919B] Perfect Number
题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...
- 并发学习一、MPI初步认识
学习参考地址:https://www.jianshu.com/p/2fd31665e816 编程使用的vs2015 社区版本(个人感觉比Vc6.0的丑界面看起来舒服多了) MPI基本函数 MPI调用借 ...
- Jdk在window环境下的安装与配置详解
本文为博主原创,转载请注明出处: 1.2 Java程序开发环境的配置 java开发工具包:java开发工具:记事本 IDE,这个只能写小程序,写大程序需要集成开发工具:反编译工具(我们可以在网上找一 ...
- BZOJ 1064: [Noi2008]假面舞会(dfs + 图论好题!)
http://www.lydsy.com/JudgeOnline/problem.php?id=1064 题意: 思路: 考虑以下几种情况: ①无环并且是树: 无环的话就是树结构了,树结构的话想一下就 ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 关于js中splice方法返回的结果
一.前言 刚刚在使用splice()方法,发现这个方法返回的是删除后的数组元素,如果要获取删除指定元素后的数组,直接调用原来的数组即可!因为splice()会改变原来数组!之前对splice()方法一 ...
- _attribute_creature
生物属性控制表 comment 备注 Entry 生物ID,对就creature_template中entry Level 不等于0时改变等级为该值 Health 不等于0时改变生命值为该值 Atta ...
- ngui项目花屏问题
项目用的ngui 最近在金立手机上遇到一个问题就是 启动的时候会花一下屏幕 一闪而过 由于ngui默认camera使用的是clear depth 所以按照网上的办法修改 color 跟skybo ...