求助
查看原帖
求助
750365
FarmarJohn楼主2024/1/27 14:00

rt

#include<bits/stdc++.h>
using namespace std;
int n,a,b,c[205],tmp;
bool vis[500005];
long long ans;
struct queue
{
	int l,r;
	int g[500005];
	queue()
	{
		l=1,r=0;
	}
	inline bool empty()
	{
		return l>r;
	}
	inline int front()
	{
		return g[l];
	}
	inline void push(int x)
	{
		++r;
		g[r]=x;
	}
	inline void pop()
	{
		++l;
	}
}q;
int main()
{
	scanf("%d%d%d",&n,&a,&b);
	for(int i=1;i<=n;++i)scanf("%d",&c[i]);
	q.push(a);
	vis[a]=1;
	while(!q.empty())
	{
		tmp=q.front();
		q.pop();
		if(tmp==b)
		{
			printf("%lld",ans);
			return 0;
		}
		if(tmp+c[tmp]<=n&&vis[tmp+c[tmp]]==0)
		{
			q.push(tmp+c[tmp]);
			vis[tmp+c[tmp]]=1;
		}
		if(tmp-c[tmp]>=1&&vis[tmp-c[tmp]]==0)
		{
			q.push(tmp-c[tmp]);
			vis[tmp-c[tmp]]=1;
		}
		++ans;
	}
	printf("-1");
	return 0;
}
2024/1/27 14:00
加载中...