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 ...
随机推荐
- Java 使用命令对堆线程分析
一.dump基本概念 在故障定位(尤其是out of memory)和性能分析的时候,经常会用到一些文件来帮助我们排除代码问题.这些文件记录了JVM运行期间的内存占用.线程执行等情况,这就是我们常说的 ...
- FJUT seventh的tired树上路径(01字典树)题解
思路(来自题解): 众所周知树上两个点xy的距离是deep[x]+deep[y]-deep[lca(x,y)]*2 然后我们把这个加减法换成异或,我们就会发现,deep[lca(x,y)]被消掉了 所 ...
- mysql中between...and..的使用,及时间范围的查询
博主原创,转载注明出处: 在mysql应用中,以范围进行查询的很多是以时间范围作为条件进行范围查询的,时间范围查询有 很多种写法,首先总结一下between....and...的使用方法: <s ...
- JAVA 上传文件到linux上并解压缩
package com.inborn.inshop.controller.mkt; import java.io.*; import ch.ethz.ssh2.ChannelCondition;imp ...
- BMv2 simple_switch 运行时切换P4程序
参考: [P4-dev] swapping p4 program using load_new_config and swap_configs commands BMv2 运行时切换P4程序 相关演示 ...
- HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...
- PostMan做接口自动化测试
try{ var jsonData = pm.response.json(); } catch (e) { console.log("No body"); } pm.environ ...
- Qt5_选择文件对话框
1. 类似如下代码: QString fileName = QFileDialog::getOpenFileName( this, tr("Open File"), NULL,// ...
- vue中的slot插槽
插槽,也就是slot,是组件的一块HTML模板,这块模板显示不显示.以及怎样显示由父组件来决定. 实际上,一个slot最核心的两个问题这里就点出来了,是显示不显示和怎样显示. 1.navigation ...
- x1c 2018 体验
总结一下: 2018对比2017优点: 1屏幕完爆:HDR WHD镜面屏完爆 FHD 雾面屏(污+雾,所谓的油腻感),还有色彩!正红色第一次觉得这么好看.别人看得出来看不出来我不知道,至少我能看出来非 ...