社区讨论

广搜,40pts求助

P1135奇怪的电梯参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo7y3wa7
此快照首次捕获于
2023/10/27 09:39
2 年前
此快照最后确认于
2023/10/27 09:39
2 年前
查看原帖
CPP
#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;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...