A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11341    Accepted Submission(s): 4289

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

#include<iostream>
#include<cstring>
#include<queue>
using namespace std; #define N 205
int v[N],c[N];
int n,a,b,t; struct node
{
int i,time;
}; void bfs(int x)
{
node now,tmp;
queue<node> q;
now.i=x,now.time=0;
memset(v,0,sizeof(v));
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.i==b)
{
t=0;
cout<<now.time<<endl;
return ;
}
for(int k=0;k<2;k++)
{
if(k==0) tmp.i=now.i+c[now.i];
if(k==1) tmp.i=now.i-c[now.i];
if(tmp.i>0&&tmp.i<=200&&!v[tmp.i])
{
v[tmp.i]=1;
tmp.time=now.time + 1;
q.push(tmp);
}
}
}
} int main()
{
while(cin>>n)
{
if(n==0) break;
cin>>a>>b;
for(int k=1;k<=n;k++)
cin>>c[k];
t=1;
bfs(a);
if(t) cout<<-1<<endl;
}
return 0;
}

HDU 1548 A strange lift 搜索的更多相关文章

  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 (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  5. hdu 1548 A strange lift (bfs)

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

  6. HDU 1548 A strange lift 题解

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

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

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

  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. ROW_NUMBER分页的注意事项

    之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据. like this: select * from ( select row_numb ...

  2. ROW_NUMBER() OVER函数的基本用法用法

    ROW_NUMBER() OVER函数的基本用法用法 转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法 ...

  3. Android 应用层知识纲要

    Java基础 * 面向对象 * Java集合框架 * 异常处理 * Java反射, Spring框架,通过反射实现 * 泛型, 静态变成语言 * 文件操作 Android基础 * Activity * ...

  4. Unix/Linux 'dirctory tree' command.

    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...

  5. 测试gcc的优化选项

    一.测试准备及原理 测试代码: static void wait(volatile unsigned long dly) { ; dly--); } int main(void) { unsigned ...

  6. HTML DOM(一):认识DOM

     分类: HTML/JavaScript/CSS(10)  版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/ghsau. 什么是DOM?       通过 Jav ...

  7. go bufio

    1 bufio 读结构 type Reader struct {    buf          []byte     // 缓冲区    rd           io.Reader // read ...

  8. 转:windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)

    原文来自于:http://www.cnblogs.com/zhuyp1015/archive/2012/06/16/2552269.html 使用gvim在windows环境下搭建简单的IDE环境可以 ...

  9. html中混入的特殊字符

    从设计那里拿来的psd中常常会有全角的单双引号. 如果只是拷贝这些字符到做好的html里面,顶多看到乱码再加以改正. 但是,如果是通篇的doc,需要加上各种html语义标签,在拷贝来的doc文字之间加 ...

  10. 通用GPIO模拟串口,提供源代码,本人经过测试OK(第一版)

    --------------------------serial.h------------------------------------------ #ifndef _SERIAL_H_ #def ...