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 ...
随机推荐
- Nvme固体硬盘Intel750,SM961分别使用一段时间以后对比
在SM961使用了一年半(2017年1月17日购买)后,再次测试,这次测试使用AS_SSD_Benchmark工具进行测试 感觉CrystalDiskMark工具测出来的分数在所以工具中分数最高 看图 ...
- ElasticSearch实战——.Net Core中的应用
dll引用: NLog.Targets.ElasticSearch,版本:4.0.0-beta26 Nlog,版本:4.5.0-rc04 Microsoft.Extensions.Configurat ...
- SPA中,Node路由优先级高于React路由
一.问题描述 在一场面试中,面试官问到了React和Node路由之间的关系. 现在SPA(单页面应用)的使用越来越广. Node(后台)和React(前端)都有自己的路由,当我页面访问一个URL的时候 ...
- 理解git的分支原理,更好地使用git
文章内容转载于git-scm. 部分内容涉嫌枯燥 一.git分支概念 几乎每一种版本控制系统都以某种形式支持分支.使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作.在很多版本控 ...
- Javascript 垃圾回收机制
转载于https://www.cnblogs.com/zhwl/p/4664604.html 一.垃圾回收的必要性 由于字符串.对象和数组没有固定大小,所有当他们的大小已知时,才能对他们进行动态的存储 ...
- zipkin启动报错(Caused by: java.lang.ClassNotFoundException: zipkin.Component)的解决方法
使用ziplin依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifact ...
- hdu-5707-Combine String
题意:给你三个字符串,让你计算1 2 串和3 串是否匹配,就是3串可以分解为 1 2 串,字母顺序必须是按照1 2 串的字母前后顺序. DP代码太深奥 看不太透,这个代码比较好理解一点: #incl ...
- 使用bat文件执行sql文件
test.bat mysql -uroot -p[password] < test.sql pause test.sql CREATE DATABASE IF NOT EXISTS test_d ...
- mysql-5.6.41-winx64安装
安装包 链接:https://pan.baidu.com/s/11-Ts3SrfJViQEtdtI_ik9w 提取码:cxt3 1.解压 将下载好的mysql-5.6.41-winx64.zip的安装 ...
- 在nodejs中的集成虹软人脸识别
==虹软官网地址==http://www.arcsoft.com.cn 在官网注册账号,并且申请人脸识别激活码, 选择SDK版本和运行系统(windows/linux/android/ios) ,我们 ...