BFS MLE求助
查看原帖
BFS MLE求助
1101266
Fe_CuSO4__FeSO4_Cu楼主2024/8/15 18:14
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,index;
};	
int a[50];
int sum,n,l,r;
void bfs()
{
	queue<node>q;
	node h,t;
	h.x=a[1],h.index=1;
	q.push(h);
	h.x=0,h.index=1;
	q.push(h);
	while(!q.empty())
	{
		h=q.front(),q.pop();
		if(h.x>r) continue;
		if(h.index>n&&l<=h.x&&h.x<=r) {sum++;continue;}
		t.x=h.x,t.index=h.index+2;
		q.push(t);
		t.x=h.x+a[h.index+1],t.index=h.index+1;
		q.push(t);
	}
}
int main()
{
	std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>l>>r;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	bfs();
	cout<<sum;
	return 0;
}

样例输出不出来。

2024/8/15 18:14
加载中...