58pts求调!!!
查看原帖
58pts求调!!!
1268907
wangcm楼主2024/9/15 20:21
#include<bits/stdc++.h>
using namespace std;
struct node
{
	long long x,step;
};
int f[100201];
bool v[100010];
int n,a,b;
bool check(int i)
{
	if(i>=0&&i<=n&&!v[i]){
		return 1;
	}
	else return 0;
}
int bfs()
{
	queue<node>q;
	node it;
	it.x=a;
	it.step=0;
	v[a]=1;
	q.push(it);
	while(!q.empty()){
		node head=q.front();
		q.pop();
		if(head.x==b){
			return head.step;
		}
		if(check(head.x+f[it.x])){
			it.x=head.x+f[it.x];
			it.step=head.step+1;
			v[it.x]=1;
			q.push(it);
		}
		else if(check(head.x-f[it.x])){
			it.x=head.x-f[it.x];
			it.step=head.step+1;
			v[it.x]=1;
			q.push(it);
		}
		else return -1;
	}
}
int main()
{
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>f[i];
	}
	cout<<bfs();
	
	return 0;
}
2024/9/15 20:21
加载中...