【HDU-4277】USACO ORZ(暴搜)

直接dfs暴力,不需要减枝, 利用set进行判断重复,hash一下,转化成一个longlong的数保存就好了。

#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXD 20 + 5
typedef long long LL;
int n;
LL array[MAXD];
set<LL>vis;
LL ans;
void dfs(LL a,LL b,LL c,int cur){
    if(cur == n){
        if(a >= b && b >= c){
            if(a + b > c && a + c > b && b + c > a && a && b && c){
                LL t = c * 100000001 + b * 10001 + a;
                if(!vis.count(t)){
                    ans ++ ;
                    vis.insert(t);
                }
            }
        }
        return ;
    }
    dfs(a + array[cur],b,c,cur + 1);
    dfs(a,b + array[cur],c,cur + 1);
    dfs(a,b,c + array[cur],cur + 1);
    return ;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        vis.clear();
        for(int i = 0 ; i < n ; i++)
            scanf("%I64d",&array[i]);
        ans = 0;
        dfs(0,0,0,0);
        printf("%I64d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(【HDU-4277】USACO ORZ(暴搜))