1360 奇怪的电梯(lift)

#include<bits/stdc++.h>
using namespace std;
struct dt{
    int cs,bs;
}que[210];
int n,a,b,ans=-1;
int yd[205];
int book[205];
int f,r;
bool fs=false;
void move(int x,int y){
    if(x>=0 && x<=200 && book[x]==0){
        book[x]=1;
        r++;
        que[r].cs=x;
        que[r].bs=y+1;
    }
}
int main(){
    memset(book,0,sizeof(book));
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++){
        cin>>yd[i];
    }
    f=r=1;
    que[r].cs=a , que[r].bs=0;
    
    while(f<=r){
        int nx,nb;
        nx=que[f].cs,nb=que[f].bs;
        if(nx==b){
            ans=nb;
            break;
        }
        move(nx+yd[nx] , nb);
        move(nx-yd[nx] , nb);
        f++;
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2020-10-06 19:50  ssdaeda  阅读(211)  评论(0编辑  收藏  举报