首页 > 其他 > 详细

奇怪的电梯

时间:2018-06-19 22:35:14      阅读:218      评论:0      收藏:0      [点我收藏+]

题面     http://ybt.ssoier.cn:8088/problem_show.php?pid=1360

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int q[32768],k[32768],a[32768];
bool b[32768];

int main()
{
    memset(b,0,sizeof(b));
    int x,y,n;
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1;i<=n;i++)
    scanf("%d",&k[i]);
    q[1]=x;
    b[x]=1;
    int h=0,t=1;
    do
    {
        h++;
        for(int i=-1;i<=1;i++)
        {
            if((q[h]+k[q[h]]*i>0&&q[h]+k[q[h]]*i<=n)&&!b[q[h]+k[q[h]]*i])
            {
                t++;
                q[t]=q[h]+k[q[h]]*i;
                b[q[h]+k[q[h]]*i]=1;
                a[t]=a[h]+1;
            }
        }
    }while(h!=t&&q[h]!=y);
    if(x==y)cout<<"0"<<endl;
    else if(q[h]==y)cout<<a[h]<<endl;
    else cout<<"-1"<<endl;
    return 0;
}

 

奇怪的电梯

原文:https://www.cnblogs.com/xrj1229/p/9201308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!