hdu 4277 USACO ORZ dfs+hash

本文介绍了一道USACOORZ竞赛题目,任务是利用给定长度的围栏段构建不同类型的三角形牧场。文章提供了完整的源代码实现,并详细解释了如何通过递归深度优先搜索算法来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)



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
题意:用所有点组成三角形,问不同的个数;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=2e5+10,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;
const double eps=(1e-8),pi=(4*atan(1.0));

int num[N],n;
struct hashnum
{
    const static int si=1e6+7;
    vector<LL>v[1000010];
    void add(LL x)
    {
        LL temp=x;
        x%=si;
        for(int i=0;i<v[x].size();i++)
            if(v[x][i]==temp)return;
        v[x].push_back(temp);
    }
}hs;
void dfs(int pos,int a,int b,int c)
{
    if(pos>n)
    {
        if(a>b)swap(a,b);
        if(b>c)swap(b,c);
        if(a>b)swap(a,b);
        if(a+b>c)
        {
            hs.add(1LL*a*10000000+b);
        }
        return;
    }
    dfs(pos+1,a+num[pos],b,c);
    dfs(pos+1,a,b+num[pos],c);
    dfs(pos+1,a,b,c+num[pos]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<=hs.si;i++)
            hs.v[i].clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        dfs(1,0,0,0);
        int ans=0;
        for(int i=0;i<hs.si;i++)
            ans+=hs.v[i].size();
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/7575553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值