hdu 4277 USACO ORZ DFS

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3581    Accepted Submission(s): 1196


Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments.
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
 

 

Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
 

 

Output
For each test case, output one integer indicating the number of different pastures.
 

 

Sample Input
1 3 2 3 4
 

 

Sample Output
1
 
 
题意
给你n个棍子,棍子可以接起来,然后问你能够拼成多少个完全不同的三角形
 
 
题解
3^15=14 348 907,所以直接暴力然后hash一下就好
 
 
 
代码
 
int aa[maxn];

set<int> kiss;

int n;

void dfs(int a,int b,int c,int d)

{

    if(d==n)

    {

        if(a*b*c*d==0)

            return;

        if(a+b>c&&a<=b&&b<=c)

        {

            kiss.insert(a*1000010000+b*10000+c);

        }

        return;

    }

    dfs(a+aa[d],b,c,d+1);

    dfs(a,b+aa[d],c,d+1);

    dfs(a,b,c+aa[d],d+1);

}

int main()

{

    int t;

    RD(t);

    while(t--)

    {

        kiss.clear();

        RD(n);

        REP(i,n)

        {

            RD(aa[i]);

        }

        dfs(0,0,0,0);

        cout<<kiss.size()<<endl;

    }

}

 

你可能感兴趣的:(USACO)