std
#include<cstdio> #include<iostream> using namespace std; int n,a,b,ans=0x7f7f7f7f; int c[205]; bool vis[205]; void dfs(int x,int bs) { if(bs>ans) return; if(x==b) ans=min(ans,bs); vis[x]=true; if(x+c[x]<=n&&!vis[x+c[x]]) dfs(x+c[x],bs+1); if(x-c[x]>=1&&!vis[x-c[x]]) dfs(x-c[x],bs+1); vis[x]=false; } int main() { scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) scanf("%d",&c[i]); vis[a]=1; dfs(a,0); if(ans!=0x7f7f7f7f) printf("%d",ans); else printf("-1"); return 0; }
data
input
200 1 200
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output
-1
wrong
101372