#include<bits/stdc++.h>
using namespace std;
int n,a,b,k[10000],x,d[10000],c[10000],x1;
queue<int>Q;
int bfs(int u)
{
Q.push(u);
while(!Q.empty())
{
x=Q.front();
Q.pop();
for(int i=0;i<2;i++)
{
int fx[2]={k[x],-k[x]};
if(x+fx[i]<1||x+fx[i]>n||d[x+fx[i]]&&c[x+fx[i]]<c[x]+1)continue;
x1=x+fx[i];
d[x1]=1;
c[x1]=c[x]+1;
if(x1==b)return c[x1];
Q.push(x1);
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>a>>b;
for(int i=1;i<=n;i++)cin>>k[i];
x1=a;
if(a==b)cout<<0;
else cout<<bfs(a);
}
https://www.luogu.com.cn/problem/solution/P1135 这代码改了很多次,MLE太狠了,这次有没有dalao? 一定关注