广搜,40pts求助
查看原帖
广搜,40pts求助
568232
Bcakspace楼主2022/9/28 16:21
#include<bits/stdc++.h>
using namespace std;
int n,a,b,k[2010],q[2010][2],hd,t1;
bool f[2010];
int main(){
    scanf("%d%d%d",&n,&a,&b);
    for(int i=1;i<=n;++i)
    scanf("%d",&k[i]);
    if(a==b){
        puts("0");
        return 0;
    }
    hd=0,t1=1;
    q[1][0]=a,q[1][1]=0;
    f[a]=true;
    while(hd<t1){
        ++hd;
        for(int i=-1;i<=1;i+=2){
            int y=q[hd][0]+i*k[q[hd][0]];
            if(y<1||y<n||f[y]) continue;
            if(y==b){
                printf("%d",q[hd][1]+1);
                return 0;
            }
            f[y]=true;
            ++t1;
            q[t1][0]=y;
            q[t1][1]=q[hd][1]+1;
        }
    }
    puts("-1");
    return 0;
}
2022/9/28 16:21
加载中...