40分求助
查看原帖
40分求助
263314
阿布鲁派楼主2021/4/9 21:40
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int n,pp,vis[205],a[205],step=-1,num;
struct node{
	int cnt,i;
};
node start,next;
queue<node>q;
void bfs(){
	while(!q.empty()){
		start=q.front();
		q.pop();
		if(start.i==pp)return;
		next.i=start.i+a[start.i];
		if(next.i<=n&&vis[next.i]==0){
			next.cnt=start.cnt+1;
			q.push(next);
			vis[next.i]=1;
		}
		next.i=start.i-a[start.i];
		if(next.i>0&&vis[next.i]==0){
			next.cnt=start.cnt+1;
			q.push(next);
			vis[next.i]=1;
		}
	}
}
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=read();start.i=read();pp=read();
	for(int i=1;i<=n;i++)
		a[i]=read();
	start.cnt=0;
	q.push(start);
	vis[start.i]=1;
	bfs();
	if(next.i==pp)cout<<next.cnt;
	else printf("-1");
	return 0;
}
2021/4/9 21:40
加载中...