30..................
查看原帖
30..................
1556684
Liao_Zongze楼主2025/1/23 14:35

求调!

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int ans[250];//下标,楼层,至少按几次按钮
int que[250];//队列,下标:序列
int l,r;//指向队列起点、终点的指针
int shuzi[250];//每个电梯的数字
int main(){
    cin>>n>>a>>b;
    memset(ans,127,sizeof(ans));//都无法到达
    for (int i=1;i<=1;i++){
        cin>>shuzi[i];
    }
    ans[a]=0;//起点至少按0次按钮
    l=1;
    que[l]=a;//将a点入队
    r=2;//准备存入扩散点
    while(l<r){
        int x=que[l];//扩散点
        int y=x+shuzi[x],z=x-shuzi[x];
        if(y<=n){
            if (ans[y]>ans[x]+1){
                ans[y]=ans[x]+1;
                que[r]=y;//目标点入队列
                r++;//终点指针+1
            }
        }
        if (z>0){
            if (ans[z]>ans[x]+1){
                ans[z]=ans[x]+1;
                que[r]=z;//目标点入队列
                r++;//终点指针+1
            }
        }
        l++;
    }
    if (ans[b]==ans[0])  {
		cout<<-1;
    }
    else{
    cout<<ans[b];
}
    return 0;
}
//AC
2025/1/23 14:35
加载中...