首页 > 其他 > 详细

hdu4277 dfs+set判重

时间:2015-03-22 19:32:30      阅读:277      评论:0      收藏:0      [点我收藏+]

http://acm.hdu.edu.cn/showproblem.php?pid=4277



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
/**
hdu4277  dfs+set判重
题目大意:给定一些一定长度的线段,要求全部利用这些线段能拼成多少种三角形(如果两个三角形至少有一条边长度不等那么二者视为两种)
解题思路:O(3^n) dfs,对于所有满足情况的三角形加入set中,这样就去重了,最后set的大小就是答案
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;

int n,ans,a[20];
set<LL> s;

void dfs(int cnt,int x,int y)
{
    int z=ans-x-y;
    if(cnt==n)
    {
        if(x<=y&&y<=z&&x+y>z)
        {
            s.insert(x*10000000+y);
        }
        return;
    }
    dfs(cnt+1,x+a[cnt],y);
    dfs(cnt+1,x,y+a[cnt]);
    dfs(cnt+1,x,y);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        s.clear();
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            ans+=a[i];
        }
        dfs(0,0,0);
        printf("%d\n",s.size());
    }
    return 0;
}


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

hdu4277 dfs+set判重

原文:http://blog.csdn.net/lvshubao1314/article/details/44540169

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!