您的位置:首页 > 其它

A strange lift(解题报告)

2014-01-14 17:11 225 查看
A strange lift

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u

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层楼梯,每层按钮的up和down数值都不同,求从a层到b层所需按按钮的最少次数。
     运用广搜、队列思想,特别注意要求如果输入0的话结束输入,所以第一次输入n时在循环外单独输入,接下来n的输入都放在循环体的最后,
     以便于对程序能否进行作出判断。开s数组来存储每层电梯能够up或down的数值,然后在Bfs(广搜)函数中把s传进去。重头戏全在Bfs函
     数里,运用下标控制入队(front)和出队(rear),记得标记函数V要先初始化(memset),循环条件是一个很值得注意的地方,有两种
     跳出循环的条件,即找到了b值(到达要到的层数)或者队列前后下标相等,若是前者,返回此时的计数函数num,后者依题意返回-1。循环
     体里主要注意下不要越界(x>=1&&x<=n)和判断是否做过标记,重复入队、出队、标记、计数操作即可。

解题代码:
#include <stdio.h>
#include <string.h>
int q[1000];
int v[1000];
int num[1000];
int s[1000];

int Bfs(int a,int b,int s[],int n)
{
    int front,rear,k,t;
    memset(v,0,sizeof(v));
    front=rear=0;
    q[front]=a;
    v[a]=1;
    num[rear++]=0;
    while(q[front]!=b && front!=rear)
    {
        t=q[front];
        k=num[front];
        if(t+s[t]<=n&&!v[t+s[t]]&&t+s[t]<1000)
        {
            q[rear]=t+s[t];
            v[t+s[t]]=1;
            num[rear++]=k+1;
        }
        if(t-s[t]>=1&&!v[t-s[t]])
        {
            q[rear]=t-s[t];
            v[t-s[t]]=1;
            num[rear++]=k+1;
        }
        front++;
    }
    if(q[front]==b)
        return num[front];
    else
        return -1;
}

int main()
{
    int n,a,b,c,i;
    scanf("%d",&n);
    if(n==0)
        return 0;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&s[i]);
        printf("%d\n",Bfs(a,b,s,n));
        scanf("%d",&n);
        if(n==0)
            break;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: