手写线性表,RE错误,76分,帮忙看看为什么
查看原帖
手写线性表,RE错误,76分,帮忙看看为什么
1026720
yunfeng18867420023楼主2024/3/11 21:29
#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<cstring>
#include <algorithm>
#include<iomanip>
#include<iostream>
using namespace std;

//---------------1135-----------
#define ListSize 50000
int n,a,b;
int k[300];
int linkList[ListSize];
int head=0,tail=0;
int step;
bool vis[300];

int main()
{
    cin>>n>>a>>b;

    for(int i=1;i<=n;i++)
    {
        cin>>k[i];
    }
    linkList[tail++]=a;
    linkList[tail++]=-1;//分层标志
    vis[a]=true;
    while(true)
    {
        if(head==tail)//队列空了
            break;
        int h=linkList[head++];
        //printf("h:%d\n",h);
        if(h==-1)//分层标志
        {
            linkList[tail++]=-1;
            step++;
            continue;
        }
        if(h==b)
        {
            cout<<step;
            return 0;
        }
        if(h+k[h]<=n&&vis[h+k[h]]==false)
        {
            //printf("check1\n");
            linkList[tail++]=h+k[h];
            vis[h+k[h]]=true;
        }
        if(h-k[h]>0&&vis[h-k[h]]==false)
        {
            //printf("check2\n");
            linkList[tail++]=h-k[h];
            vis[h-k[h]]=true;
        }
    }

    cout<<"-1";
    return 0;
}

2024/3/11 21:29
加载中...