YbtOJ 深度搜索课堂过关 例1 拔河比赛【深度优先搜索】

在这里插入图片描述


思路

这道题是一道比较基础的深搜题
对于每一个队员,只有选1队和选2队两种状态。
搜索每一种情况取最小即可。

C o d e Code Code

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int w[100010];
int T,n,ans=2147483647;
void dfs(int x,int g1,int g2,int js1,int js2)
{
    
    
	if(x>n)
	 {
    
    
	 	if(abs(g1-g2)<=1)
	 	 {
    
    
	 	 	if(ans>abs(js1-js2))
			  ans=abs(js1-js2);
	 	 }
	 	return;
	 }
	dfs(x+1,g1+1,g2,js1+w[x],js2);
	dfs(x+1,g1,g2+1,js1,js2+w[x]);
}
int main()
{
    
    
	cin>>T;
	while(T--)
	 {
    
    
	 	cin>>n;
	 	for(int i=1; i<=n; i++)
	 	   scanf("%d",&w[i]);
	 	dfs(1,0,0,0,0);
	 	cout<<ans<<endl;
	 	ans=2147483647;
	 }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jackma_mayichao/article/details/112118888
今日推荐