30分求调!!!
查看原帖
30分求调!!!
1376899
Powerful_K楼主2025/2/1 15:56
#include<bits/stdc++.h>
using namespace std;
struct node{
	int ceng,ci;
};
queue<node> q;
int a,b,n,k[2000],mark[2000];
int main(){
	cin >> n >> a >> b;
	for(int i=1;i<=n;i++){
		cin >> k[i];
	}
	q.push(node{a,0});
	mark[a]=1;
	node c;
	while(!q.empty()){
		c=q.front();
		q.pop();
		if(c.ceng==b){
			break;
		}
		for(int i=-1;i<=1;i+=2){
			int x=c.ceng+i*k[c.ceng];
			if(x>=1&&x<=n&&mark[x]==0){
				q.push(node{x,c.ceng+1});
				mark[x]=1;
			}
		}
	}
	if(c.ceng==b){
		cout << c.ci << endl;
	}
	else{
		cout << -1 << endl;
	}
	return 0;
}
2025/2/1 15:56
加载中...