蒟蒻求助,和部分点正确答案差1,找不出问题出在哪里
查看原帖
蒟蒻求助,和部分点正确答案差1,找不出问题出在哪里
243583
huino楼主2024/2/26 22:54
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int main(){
	queue<long long> q;
	int N, A, B;
	cin>>N>>A>>B;
	long long a[N+1], b[N+1], c[N+1];
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	for(int i=1;i<=N;i++){
		cin>>a[i];
	}
	q.push(A);
	while(!q.empty()){
		if(b[q.front()] == 0){
			b[q.front()] = c[q.front()];
			if(q.front()+a[q.front()] <= N){
				q.push(q.front()+a[q.front()]);
				c[q.front()+a[q.front()]] = c[q.front()]+1;
			}
				
			if(q.front()-a[q.front()]>0){
				q.push(q.front()-a[q.front()]);
				c[q.front()-a[q.front()]] = c[q.front()]+1;
			}
				
		}
		q.pop();
		if(b[B] != 0)
			break;
		
	}
	if(b[B] != 0)
		cout<<b[B];
	else cout<<-1;
	return 0;
}
2024/2/26 22:54
加载中...