# A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14882    Accepted Submission(s): 5633

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

bfs code：
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int flor[210],n,s,t,vis[210];
struct node {
int ord,ste;
};
int judge(node tem)
{
if(tem.ord<=0 || tem.ord>n)
return 0;
return 1;
}
int bfs()
{
queue<node> q;
node tem,next;
node a;
a.ord=s;
vis[s]=1;
a.ste=0;
q.push(a);
while(!q.empty())
{
tem=q.front();
q.pop();
if(tem.ord==t)
return tem.ste;
next.ord=tem.ord+flor[tem.ord];
if(judge(next) && !vis[next.ord])
{
next.ste=tem.ste+1;
q.push(next);
vis[next.ord]=1;
}
next.ord=tem.ord-flor[tem.ord];
if(judge(next) && !vis[next.ord])
{
next.ste=tem.ste+1;
q.push(next);
vis[next.ord]=1;
}
}
return -1;
}
int main()
{
//freopen("input.txt","r",stdin);
int i,j;
while(scanf("%d",&n),n)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&s,&t);
for(i=1;i<=n;i++)
scanf("%d",&flor[i]);
printf("%d\n",bfs());
}
return 0;
}
dij code：
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
const int inf = 1<<30;
int map[210][210],dis[210],vis[210],n,s,t;
void dij()
{
int i,j;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
dis[s]=0;
vis[s]=1;
int t;
for(i=1;i<n;i++)
{
int minn=inf;
for(j=1;j<=n;j++)
{
if(dis[j]<minn && vis[j]==0)
{
t=j;
minn=dis[j];
}
}

if(minn==inf)
break;
vis[t]=1;

for(j=1;j<=n;j++)
{
if(dis[j]>dis[t]+map[t][j] && !vis[j])
dis[j]=dis[t]+map[t][j];
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
int i,j,ttt,tt;
while(scanf("%d",&n),n)
{
scanf("%d%d",&s,&t);
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
map[i][j]=inf;
if(i==j)
map[i][j]=0;
}
for(i=1; i<=n; i++)
{
scanf("%d",&ttt);
if(i+ttt<=n)
map[i][i+ttt]=1;
if(i-ttt>0)
map[i][i-ttt]=1;
}
dij();
printf("%d\n",dis[t]==inf?-1:dis[t]);
}
return 0;
}


#### HDU1548-A strange lift-最短路(Dijkstra模板题)

2015-08-29 17:02:28

#### BFS(广搜)算法与模板 bfs求最短路

2017-02-10 15:32:47

#### 用BFS找最短路，并打印路径

2011-03-29 13:11:00

#### 图--用BFS求最短路

2018-01-29 16:48:04

#### bfs和spfa最短路算法的区别，细节

2013-12-01 21:46:45

#### POJ 3170（bfs求最短路）

2016-07-28 21:44:05

#### BFS求解最短路经典算法

2017-06-12 13:52:24

#### hdu1548(BFS广搜入门)

2016-03-01 23:32:39

#### 图的遍历（dfs、bfs、最短路、最小生成树、拓扑排序）

2016-11-06 21:48:46

#### hdu oj 2544 最短路(最短路径）

2014-08-10 15:16:25