2012 ACM/ICPC Asia Regional Changchun Online(USACO ORZ )

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个数,n个数自由组很形成不同的三角形有多少种?
解题思路:直接暴力搜索用<set>容器判重
#include<stdio.h>
#include<set>
#include<iostream>
using namespace std;
int arr[20];
int n,cnt;
set<long long> s;
void dfs(int a,int b,int c,int k)
{
    if(k==n)
    {
        if(a>b||b>c||a>c) return;
        if(a&&b&&c&&a+b>c)
        {
            s.insert(1000000*a+100000*b+c);//防止各不不同的数得到值相同的情况
        }
        return;
    }
    dfs(a+arr[k],b,c,k+1);
    dfs(a,b+arr[k],c,k+1);
    dfs(a,b,c+arr[k],k+1);
}
int main()
{
    int test,i;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&arr[i]);
        }
        s.clear();
        dfs(0,0,0,0);
        printf("%d\n",s.size());
    }
    return 0;
}
posted @ 2013-06-17 16:37  forevermemory  阅读(189)  评论(0编辑  收藏  举报