#9求助
查看原帖
#9求助
401154
nickxuan楼主2021/2/15 09:41
#include<bits/stdc++.h>
using namespace std;
int k[201],c[201];
int n,a,b;
bool f[1000];
queue<int> q;
int kkk(int a)
{
	q.push(a);
	while(!q.empty())
	{
		int x=q.front();
		f[x]=true;
		q.pop();
		int x1=x+k[x];
		int x2=x-k[x];
		if(x1>0&&x1<=n&&!f[x1])
		{
			q.push(x1);
			c[x1]=c[x]+1;
		}
		if(x2>0&&x2<=n&&!f[x2])
		{
			q.push(x2);
			c[x2]=c[x]+1;
		}
		if(c[b]>0) return c[b];
	}
	return -1;
}
int main()
{
	cin>>n>>a>>b;
	if(a==b) 
	{
		cout<<"0";
		return 0;
	}
	for(int i=1;i<=n;i++) cin>>k[i];
	cout<<kkk(a);
	return 0;
}
2021/2/15 09:41
加载中...