求助,只拿了70分
查看原帖
求助,只拿了70分
974546
studychen楼主2023/12/21 16:09
#include<bits/stdc++.h>
using namespace std;
typedef struct{
	int x,step;
}node;
node st,de;
int N,A,B,a[505];
bool vis[505];
int bfs(int s,int e){
	queue<node>q;
	q.push(st);
	vis[s]=true;
	while(!q.empty()){
		node now=q.front();
		q.pop();
		if(now.x==e)	return now.step;
		de.x=now.x+a[now.x],de.step=now.step+1;
		if(de.x>=1&&de.x<=B&&!vis[de.x]){
			q.push(de);
			vis[de.x]=true;
		}
		de.x-=2*a[now.x];
		if(de.x>=1&&de.x<=B&&!vis[de.x]){
			q.push(de);
			vis[de.x]=true;
		}
	}
	return -1;
}
int main(){
	cin>>N>>A>>B;
	for(int i=1;i<=N;i++)	cin>>a[i];
	st.x=A,st.step=0;
	cout<<bfs(A,B);
}
2023/12/21 16:09
加载中...