求助,#9RE
查看原帖
求助,#9RE
222555
Simon_Lu楼主2022/5/26 23:23

rt

#include<bits/stdc++.h>
using namespace std;
int n,a,b,head,tail,k[205];
bool book[205];
struct stair{
    int pos,step;
    stair(){}
    stair(const int &_pos,const int &_step):pos(_pos),step(_step){}
}q[8000005];
void bfs(int start,int end){
    if(start==end){
        cout<<0;
        exit(0);
    }
    book[start]=1;
    head=tail=1;
    q[tail++]=stair(start,0);
    while(head<tail){
        stair old=q[head++];
        for(int i=0;i<=1;i++){
            if(!i){
                int neww=old.pos+k[old.pos];
                if(book[neww]||neww<1)
                    continue;
                if(neww==end){
                    cout<<old.step+1;
                    exit(0);
                }
                q[tail++]=stair(neww,old.step+1);
                book[neww]=1;
            }
            else{
                int neww=old.pos-k[old.pos];
                if(book[neww]||neww<1)
                    continue;
                if(neww==end){
                    cout<<old.step+1;
                    exit(0);
                }
                q[tail++]=stair(neww,old.step+1);
                book[neww]=1;
            }
        }
    }
}
int main(){
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++)
        cin>>k[i];
    bfs(a,b);
    cout<<-1;
    return 0;
}
2022/5/26 23:23
加载中...