DFS。
等同于把长度数组划分成和相等的4份。
#include <iostream>
#include <string.h>
using namespace std;
int n;
int len[20];
bool visit[20];
int sum;
int side;
bool dfs(int cur,int num, int cur_len){
if(num == 3)
return true;
for(int i=cur;i<n;++i){
if(visit[i])
continue;
visit[i] = true;
if(cur_len + len[i]<side){
if(dfs(i+1,num,cur_len+len[i]))
return true;
}
else if(cur_len + len[i]==side){
if(dfs(0,num+1,0))
return true;
}
visit[i] = false;
}
return false;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n;
memset(visit,0,sizeof(visit));
sum = 0;
for(int i=0;i<n;++i){
cin>>len[i];
sum += len[i];
}
if(sum%4!=0){
cout<<"no"<<endl;
continue;
}
side = sum/4;
if(dfs(0,0,0))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}