30分求助qwq
查看原帖
30分求助qwq
967055
bajia楼主2024/8/11 12:10

对的测试点:6,7,8,10,13。其余WA

#include<iostream>
#include<queue>
using namespace std;
struct node{
	int n,k,l;
};
int main(){
	queue<node> q;
	node o[500],t;
	bool s[500];
	int n,a,b;
	cin>>n>>a>>b;
	for(int i=0;i<n;i++){
		cin>>o[i].k;
		o[i].n=i+1;
		if(i+1==a) s[i]==false;
		else s[i]==true;
	}
	t.n=a;
	t.k=o[a-1].k;
	t.l=0;
	q.push(t);
	while(!q.empty()){
		node top=q.front();
		s[q.front().n]=false;
		q.pop();
		if(b==top.n){
			cout<<top.l;
			return 0;
		}
		if(top.k<top.n&&s[top.n-top.k-1]){
			o[top.n-top.k-1].l=top.l+1;
			q.push(o[top.n-top.k-1]);
		}
		if(top.k+top.n<=n&&s[top.n+top.k-1]){
			o[top.n+top.k-1].l=top.l+1;
			q.push(o[top.n+top.k-1]);
		}
	}
	cout<<-1;
	return 0;
}
2024/8/11 12:10
加载中...