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"?
 
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.
 
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 5
3 3 1 2 5
0
 
Sample Output
3
 
 
题意:一个n层的奇怪的电梯,每层有两个键,上、下。在第i层有一个数字啊a[i],表示能从这一层到第i-a[i]层和第i+a[i]层。问从指定层到指定层的最少按键次数。
解析:两种方法。BFS和单源最短路。
 
代码如下:
 
BFS:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int pos,time;
};
int n,a,b;
int f[];
int mark[];
void BFS()
{
queue<node>q;
memset(mark,,sizeof(mark));
node now;
now.pos=a,now.time=;
q.push(now);
mark[a]=;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.pos==b){
printf("%d\n",now.time);
return ;
}
node nxt;
nxt.pos=now.pos+f[now.pos];
nxt.time=now.time+;
if(nxt.pos<=n&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
nxt.pos=now.pos-f[now.pos];
nxt.time=now.time+;
if(nxt.pos>=&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
}
printf("-1\n");
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",&f[i]);
BFS();
}
}

spfa:

 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=<<;
int mp[][];
int f[],a,b,n,dis[];
void init()
{
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
mp[i][j]=(i==j)?:INF;
for(int i=;i<=n;++i){
if(i+f[i]<=n)
mp[i][i+f[i]]=;
if(i-f[i]>=)
mp[i][i-f[i]]=;
}
}
void spfa()
{
init();
fill(dis+,dis+n+,INF);
queue<int>q;
q.push(a);
dis[a]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<=n;++i){
if(dis[i]>dis[u]+mp[u][i]){
dis[i]=dis[u]+mp[u][i];
q.push(i);
}
}
}
if(dis[b]==INF)
printf("-1\n");
else
printf("%d\n",dis[b]);
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",f+i);
spfa();
}
return ;
}
 

HDU-1548 A strange lift(单源最短路 或 BFS)的更多相关文章

  1. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

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

  3. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

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

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

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

  6. HDU 1548 A strange lift 搜索

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

  7. hdu 1548 A strange lift (bfs)

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

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

  9. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

随机推荐

  1. 含有虚函数的类sizeof大小

    #include <iostream> using namespace std; class Base1{ virtual void fun1(){} virtual void fun11 ...

  2. 一个远程启动windows c++程序引发的技术决策现象

    还是因为那个8点半前要启动近百套报盘程序的问题,差不多两周前表示自己会抽空给解决掉,一次性启动,直到昨天才差不多能够抽点时间出来开始想怎么解决的问题. 这个问题的复杂点在于除了启动exe外,还需要鼠标 ...

  3. django admin 使用

    Django admin 中一些常用的设置 Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸索 ...

  4. Android java 多线程(三)

  5. TensorFlow入门(四) name / variable_scope 的使

    name/variable_scope 的作用 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-08 refer to: ...

  6. Win32 API编程:使用CreateProcess创建新进程

    #include <windows.h> #include <tchar.h> #include <stdio.h> int main(int argc, char ...

  7. Python3基础 raise 产生RuntimeError 异常

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Bootloader之uBoot简介

    本文转载自:http://blog.ednchina.com/hhuwxf/1915416/message.aspx 一.Bootloader的引入 从前面的硬件实验可以知道,系统上电之后,需要一段程 ...

  9. centos7下yum升级被PackageKit锁定

    新安装centos7后,第一次升级出现下面的错误: Another app is currently holding the yum lock; waiting for it to exit... 另 ...

  10. Excel中Application和ApplicationClass的区别

    Application和ApplicationClass的联系和区别Application和ApplicationClass都继承自接口_Application.Application为接口.Appl ...