hdu 4277 USACO ORZ (dfs+hash)

USACO ORZ

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


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=15,每层扩展3个节点,感觉暴搜肯定会超时,肯定要剪枝。于是想各种剪枝,想到思维混乱了。下来看了一下别人博客,说暴搜就够了,哎。。。!比赛时太胆小了,连暴搜都没有尝试过。 。。

代码:
// Exe.Time  890MS   Exe.Memory   5860K 
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 20
#define maxx 1000000+7
using namespace std;

int n,m,ans;
int a[maxn];
long long sum;
long long hash[maxx];

void hashadd(long long s)
{
    int t;
    t=s%maxx;
    while(hash[t]!=-1&&hash[t]!=s)
    {
        t+=10;
        if(t>=maxx) t=t%maxx;
    }
    if(hash[t]==-1)
    {
        hash[t]=s;
        ans++;
    }
}
void dfs(int l1,int l2,int l3,int pos)
{
    if(pos>n)
    {
        if(l1<=l2&&l2<=l3&&l1+l2>l3)
        {
            long long t=l1+l2*sum+l3*sum*sum;
            hashadd(t);
        }
        return ;
    }
    dfs(l1+a[pos],l2,l3,pos+1);
    dfs(l1,l2+a[pos],l3,pos+1);
    dfs(l1,l2,l3+a[pos],pos+1);
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        ans=0;
        memset(hash,-1,sizeof(hash));
        dfs(0,0,0,1);
        printf("%d\n",ans);
    }
    return 0;
}


 哎 比赛时把重点放在剪枝上面了  没想到用hash来判重  所以WA了  下来加一个判重就过了  
代码:
// Exe.Time  515MS   Exe.Memory   8092K 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <queue>
#define maxn 20
#define maxx 1000007
using namespace std;

int n,m,ans,sum;
double s2,s3;
int a[maxn];
long long ss;
long long hash[maxx];

void hashadd(long long xx)
{
    int yy=xx%maxx;
    while(hash[yy]!=-1&&hash[yy]!=xx)
    {
        yy+=10;
        if(yy>maxx) yy=yy%maxx;
    }
    if(hash[yy]==-1)
    {
        hash[yy]=xx;
        ans++;
    }
}
void dfs(int l1,int l2,int pos)
{
    int i,j;
    if(pos>n)
    {
        int l3=sum-l1-l2;
        if(l1<=l2&&l2<=l3&&l1+l2>l3)
        {
            ss=l1+(long long)l2*sum+(long long)l3*sum*sum;
            hashadd(ss);
        }
        return ;
    }
    if(l1+a[pos]<=s3&&l2+a[pos]<=s2)
    {
        dfs(l1+a[pos],l2,pos+1);
        dfs(l1,l2+a[pos],pos+1);
        dfs(l1,l2,pos+1);
    }
    else if(l1+a[pos]>s3&&l2+a[pos]>s2)   // 剪枝  如果了l1、l2都不能加了
    {
        int l3=sum-l1-l2;
        if(l1<=l2&&l2<=l3&&l1+l2>l3)
        {
            ss=l1+(long long)l2*sum+(long long)l3*sum*sum;
            hashadd(ss);
        }
        return ;
    }
    else if(l1+a[pos]<=s3&&l2+a[pos]>s2)
    {
        dfs(l1+a[pos],l2,pos+1);
        dfs(l1,l2,pos+1);
    }
    else
    {
        dfs(l1,l2+a[pos],pos+1);
        dfs(l1,l2,pos+1);
    }
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a+1,a+n+1);
        s2=sum*1.0/2.0;
        s3=sum*1.0/3.0;
        if(a[n]>=s2)
        {
            printf("0\n");
            continue ;
        }
        ans=0;
        memset(hash,-1,sizeof(hash));
        dfs(0,0,1);
        printf("%d\n",ans);
    }
    return 0;
}



 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值