#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define M 205
#define INF 0x3f3f3f3f
using namespace std; int map[M][M];
int d[M], vis[M];
int n;
int b, e; void Dijkstra(){
memset(d, 0x3f, sizeof(d));
memset(vis, , sizeof(vis));
d[b]=;
vis[b]=;
int root=b;
for(int j=; j<n; ++j){
int minLen=INF, p;
for(int i=; i<=n; ++i){
if(!vis[i] && d[i] > d[root] + map[root][i])
d[i] = d[root] + map[root][i];
if(!vis[i] && minLen>d[i]){
p=i;
minLen=d[i];
}
}
if(minLen==INF)
return;
root=p;
vis[root]=;
}
} int main(){
while(cin>>n && n){
cin>>b>>e;
memset(map, 0x3f, sizeof(map));
for(int i=; i<=n; ++i){
int x, xx;
cin>>x;
map[i][i]=;// i+(-)x 表示 从第i层可以到达第 i+(-)x层
xx=i-x;
if(xx>=) map[i][xx]=;
xx=i+x;
if(xx<=n) map[i][xx]=;
}
Dijkstra();
if(d[e]==INF)
cout<<"-1"<<endl;
else
cout<<d[e]<<endl;
}
return ;
}
 /*
思路:当前电梯位置的两个方向进行搜索!
注意:搜索时的界限, 用x表示将要到达的电梯的位置
1. 首先是x>=1
2. x<=n
3. vis[x]!=0 表明bfs时候没有经过这个楼层
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int x;
int step;
node(){}
node(int x, int step){
this->x=x;
this->step=step;
}
};
queue<node>q;
int n, b, e;
int f[];
int vis[]; bool bfs(){
while(!q.empty()) q.pop();
memset(vis, , sizeof(vis));
q.push(node(b,));
vis[b]=;
while(!q.empty()){
node cur=q.front();
q.pop();
if(cur.x==e){
cout<<cur.step<<endl;
return true;
}
int xx;
xx=cur.x+f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx<=n && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
xx=cur.x-f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx>= && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
}
return false;
} int main(){
while(cin>>n && n){
cin>>b>>e;
for(int i=; i<=n; ++i)
cin>>f[i];
if(!bfs())
cout<<"-1"<<endl;
}
return ;
}

hdu1548 A strange lift(bfs 或Dijkstra最短路径)的更多相关文章

  1. HDU1548——A strange lift(最短路径:dijkstra算法)

    A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...

  2. 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 ...

  3. HDU1548:A strange lift

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  4. hdu 1548 A strange lift (bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. Hdu1548 A strange lift 2017-01-17 10:34 35人阅读 评论(0) 收藏

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  6. 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 ...

  7. hdu1584 A strange lift (电梯最短路径问题)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  9. bfs A strange lift

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...

随机推荐

  1. 博客迁移至CSDN

    本人的技术博客已经迁移至CSDN,地址为http://blog.csdn.net/starrow,现为Lotus Domino开发领域最活跃丰富的博客,内容包括Lotus Domino, JavaSc ...

  2. web前端防治重复提交

    web前端开发中防治重复提交 web前端数据请求或者表单提交往往通过对dom的点击事件来操作,但是往往因为认为点击过快(少年手速挺快的嘛),或者因为响应等待使得用户误人为没操作而重复很多次点击,造成表 ...

  3. 8.4.1 ImageLoader

    ImageLoader 的工作原理(已经不维护了) 在显示图片的时候,它会先在内存中查找:如果没有,就去本地查找:如果还没有,就开一个新的线程去下载这张图片,下载成功会把图片同时缓存到内存和本地. 基 ...

  4. 用python2.7,采集新浪博客

    #coding=utf-8 #新浪博客 import urllib import re import os url=['']*1500 #每一骗博客的地址 title=['']*1500 #每一篇博客 ...

  5. JUnit Assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...

  6. The Rotation Game(IDA*算法)

    The Rotation Game Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 300000/150000K (Java/Othe ...

  7. 我的ORM之一 -- 查询

    我的ORM索引 概述 http://code.taobao.org/svn/MyOql/ 这是我自己写的开源ORM教程,我想先从场景示例中切入介绍,先有一个感性的认识,以小见大,触类旁通,有了这个认识 ...

  8. RCP:给GEF编辑器添加网格和标尺。

    网格和标尺效果如上图所示. 添加网格比较简单,也可以自己实现,主要思路是为编辑器添加一个GridLayer.但是还是建议参考eclipse自己的GEF样例来实现. 需要注意两个部分: 1.重写org. ...

  9. Java虚拟机6:内存溢出和内存泄露、并行和并发、Minor GC和Full GC、Client模式和Server模式的区别

    前言 之前的文章尤其是讲解GC的时候提到了很多的概念,比如内存溢出和内存泄露.并行与并发.Client模式和Server模式.Minor GC和Full GC,本文详细讲解下这些概念的区别. 内存溢出 ...

  10. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...