36pts求调
查看原帖
36pts求调
1033674
ayf2192538031楼主2024/6/9 08:50

P1135

#include<bits/stdc++.h>
using namespace std;
int n,a,b,F[300];
bool u[300];
struct node{
	int fl,st;
};
queue <node> bfs;
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>F[n];
	}
	bfs.push(node{a,0});
	u[a]=1;
	while(!bfs.empty()){
		int x=bfs.front().fl,y=bfs.front().st;
		if(x==b){
			cout<<y;return 0;
		}
		bfs.pop();
		int upf=x+F[x],downf=x-F[x];
		if((upf>0)&&(upf<=n)&&(!u[upf])){
			bfs.push(node{upf,y+1});u[upf]=1;
		}
		if((downf<=n)&&(downf>0)&&(!u[downf])){
			bfs.push(node{downf,y+1});u[downf]=1;
		}
	}
	cout<<-1;
	return 0;
}
2024/6/9 08:50
加载中...