【暴力搜索】[HDU 1518]Square
先说好,我的程序速度很慢1018ms。。其实就是算出总长度除以4然后搞一下和http://blog.csdn.net/jeremygjy/article/details/47043161一样的剪枝就好了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 20;
int n, s[MAXN+10];
bool vis[MAXN+10];
int sum ;
bool dfs(int u, int up, int _len){
//printf("%d\n", _len);
if(u == 3){return true;}
for(int i=up;i<n;i++) if(!vis[i] && s[i] + _len <= sum){
vis[i] = true;
if(s[i] + _len == sum && dfs(u+1, 0, 0)) return true;
else if(s[i] + _len < sum && dfs(u, i+1, s[i]+_len)) return true;
vis[i] = false;
}
return false;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(vis, 0, sizeof vis);
scanf("%d", &n);
sum = 0;
for(int i=0;i<n;i++){
scanf("%d", &s[i]);
sum += s[i];
}
sort(s, s+n);
//printf("%d\n", sum);
if(sum % 4 != 0){
printf("no\n");
continue;
}
sum >>= 2;
//printf("%d\n", sum);
if(dfs(0, 0, 0)){
printf("yes\n");
}else printf("no\n");
}
return 0;
}