题解 P1135 【奇怪的电梯】

· · 题解

啥都不想讲,上代码

#include<bits/stdc++.h>
using namespace std;
bool f[201];
int s[210],k[201],h[201][2],n,a,b;
int main()
{
    cin>>n>>a>>b;
    if(a==b){
        cout<<0;
        return 0;
    }//一定一定要特判
    for(int i=1;i<=n;i++)cin>>k[i];
    int t=0,w=1;
    h[1][1]=a;h[1][0]=0;
    f[a]=1;
    while(t!=w)
    {
        t++;
        int x=h[t][1]+k[h[t][1]];
        if(x<=n&&!f[x])
        {
            w++;
            f[x]=1;
            h[w][1]=x;
            h[w][0]=h[t][0]+1;
            if(x==b){
                cout<<h[w][0];return 0;
            }
        }//向上
        int y=h[t][1]-k[h[t][1]];
        if(y>0&&!f[y])
        {
            w++;
            f[y]=1;
            h[w][1]=y;
            h[w][0]=h[t][0]+1;
            if(y==b){
                cout<<h[w][0];return 0;
            }//向下
}
    }
    cout<<-1;//达不到
    return 0;
}