原题: https://www.luogu.com.cn/problem/P1135
#include<bits/stdc++.h>
using namespace std;
const int N = 500;
int n,a,b;
int flood[N];
int d[N];
int bfs(int n,int a,int b)
{
queue<int>q;
memset(d,-1,sizeof(d));
q.push(a);
d[a]=0;
while(!q.empty())
{
auto t = q.front();
q.pop();
int up =t +flood[t];
if(d[up]==-1 and up>0 and up<=n)
{
d[up] = d[t]+1;
q.push({up});
}
int down = t-flood[t];
if(d[down]==-1 and down>0 and down<=n)
{
d[down] = d[t]+1;
q.push({down});
}
}
return d[b];
}
int main()
{
cin>>n>>a>>b;
for(int i=1;i<=n;i++)
{
cin>>flood[i];
}
cout<<bfs(n,a,b);
return 0;
}