AC

***  •  24天前


include<bits/stdc++.h>

using namespace std; int n,a,b,ki,lift[205],bj[205],c[3]; struct dt{

int num,bu;

}; void bfs(int x){

queue<dt> q;
q.push({x,0});
while(q.size()!=0){
	dt tou=q.front();
	q.pop();
	c[1]=tou.num+lift[tou.num],c[2]=tou.num-lift[tou.num];
	for(int i=1;i<=2;i++)
		if(c[1]<=n&&c[1]>=1&&bj[c[i]]==0){
			bj[c[i]]=1;
			if(c[i]==b){
				cout<<tou.bu+1;return ;
			}else{
				q.push({c[i],tou.bu+1});
			}
		}
}
cout<<-1;

} int main(){

cin>>n>>a>>b;
for(int i=1;i<=n;i++) cin>>lift[i];
if(a==b){
	cout<<0;
	return 0;
}
bfs(a);
return 0;

}


评论:

记得加#


***  •  24天前

请先登录,才能进行评论