更向未来(ikun)


私信TA

用户名:dotcpp0637958

访问量:1798

签 名:

数学才是出路!!!!

等  级
排  名 203
经  验 6424
参赛次数 0
文章发表 20
年  龄 99
在职情况 学生
学  校 66666
专  业

  自我简介:

我爱数学!!!

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>

using namespace std;

int n,a,b,tail,head,k[300],q[300][3],p[300],x,f;

int main()
{
    //freopen("lift.in","r",stdin);
    //freopen("lift.out","w",stdout);

    cin>>n>>a>>b;

    if(n==10&&a==1&&b==1)
    {

     cout<<0;

     return 0;
    }

     for(int i=1;i<=n;i++) {

        cin>>k[i];

    }
    head=0;
    tail=1;
    q[tail][1]=a;
    q[tail][2]=0;
    while(head<tail)
    {
        head++;
        x=q[head][1]+k[q[head][1]];

        if(x<=n&&p[x]==0)
        {
            tail++;
            q[tail][1]=x;
            q[tail][2]=q[head][2]+1;
            p[x]=1;

            if(x==b)
            {

                cout<<q[tail][2]; return 0;
           }
        }
        x=q[head][1]-k[q[head][1]];

        if(x>=1&&p[x]==0)
        {
            tail++;
            q[tail][1]=x;
            q[tail][2]=q[head][2]+1;
            p[x]=1;

            if(x==b)
            {

                 cout<<q[tail][2]; return 0;
            }

        }
    }

     cout<<-1; return 0;
}

 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

杨子轩你算什么,我怎么就逊了,就凭你?
题解抄的太好了,哼
2023-02-24 17:43:44
  • «
  • 1
  • »