HDU 4277 USACO ORZ(DFS爆搜 + set判重复)

USACO ORZ

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


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
 

Source




   题意:有n个木棍,要求把n个木棍全部用完的情况下能组成多少种三角形,其中等边三角形算一种
   思路:将n个木棍进行随机组合,然后默认a边为三角形最短的那条边,这样可以去除一些重复的情况,在当所有木棍用完时进行判断可不可以组成三角形,可以的话把三条边按照某值进行计算,确保不同的边没有相同的值。又来盼等边三角形的重。



#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<set>

using namespace std;

int n;
int a[100100];
int sum,ans;
set<__int64>q;

void DFS(int x,int y,int z,int cnt) {
    if(x>sum/3) {
        return ;
    }
    if(cnt == n+1) {
        if(x>y || x>z || y>z || x == 0 ||y == 0 || z == 0) {
            return ;
        }
        if(x+y>z && x+z>y && y+z>x) {
            __int64 num = ((x*11113 + y)*11117 + z)*11119;
            q.insert(num);
        }
        return ;
    }
    DFS(x+a[cnt],y,z,cnt+1);
    DFS(x,y+a[cnt],z,cnt+1);
    DFS(x,y,z+a[cnt],cnt+1);
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        sum = 0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        q.clear();
        DFS(0,0,0,1);
        printf("%d\n",q.size());
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值