HDU-a strange lift-BFS

问题及代码:

Problem G  a strange lift

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.    
*文件名称:HDU.cpp       
*完成日期:2015年3月5日    
*版 本 号:v1.0        
*/ 
#include <stdio.h>
#include <string.h>
int map[210];
int bfs(int a,int b);
int queue[10000];
int mark[10000];
int step[10000];
int n;
int main()
{
    int a,b;
    while(scanf("%d",&n)&&n)
    {
        memset(map,0,sizeof(map));
        memset(mark,0,sizeof(mark));
        memset(step,0,sizeof(step));
        scanf("%d%d",&a,&b);
        int i;
        for(i=1; i<=n; i++)
            scanf("%d",&map[i]);
        if(a==b)
        {
            printf("0\n");
            continue;
        }
        int ans=bfs(a,b);
        if(ans)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}

int bfs(int a,int b)
{
    queue[1]=a;
    mark[queue[1]]=1;
    int front=1,rear=2;
    int k=a;
    while(front<rear)
    {
        k=queue[front];
        if(k+map[k]<=n&&!mark[k+map[k]])
        {
            int newl=k+map[k];
            queue[rear++]=newl;
            mark[newl]=1;
            step[newl]=step[queue[front]]+1;
            if(newl==b)
                return  step[newl];
        }
        if(k-map[k]>=1&&!mark[k-map[k]])
        {
            int newl=k-map[k];
            queue[rear++]=newl;
            mark[newl]=1;
            step[newl]=step[queue[front]]+1;
            if(newl==b)
                return step[newl];
        }
        front++;
    }
    return 0;
}


运行结果:

HDU-a strange lift-BFS_第1张图片


知识点总结:
BFS。

学习心得:

好难,给跪。

你可能感兴趣的:(C++,HDU,bfs,lift,a,strange)