求助!!!!
查看原帖
求助!!!!
638274
_YQY楼主2022/7/18 09:39
#include<bits/stdc++.h>
using namespace std;
int k[10005],vis[10005];
struct node{
	int floor,s;
};
queue<node> Q;
int main(){
	int n,a,b;
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
		cin>>k[i];
	}
	node tmp={a,0};
	vis[a]=1;
	node now;
	while(!Q.empty()){
		now=Q.front();
		Q.pop();
		if(now.floor==b) break;
		for(int d=-1;d<=1;d+=2)
		{
			int dist=now.floor+k[now.floor]*d;
			if(dist>=1&&dist<=n&&vis[dist]==0){
				node f={dist,now.s+1}; 
				Q.push(f);
				vis[dist]=1; 
			}
		
		}
	}
	if(now.floor==b)
	cout<<now.s<<endl;
	else 
	cout<<-1<<endl;
	return 0;
}
2022/7/18 09:39
加载中...