zoukankan      html  css  js  c++  java
  • HDU 4277 dfs+set去重

    USACO ORZ

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


    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根木棍,用这所有的木棍搭三角形,求不同三角形的个数,不同三角形至少某一边不相等

    思路:重点是去重,方法一是直接hash,简单hash一下就行,方法二是直接将三个数都塞进set里,然后直接统计个数




    //解法1
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<set>
    using namespace std;
    set<int>m_set;
    int n,num[30],sum;
    void dfs(int pos,int a,int b,int c)
    {
        if(pos==n)
        {
            if(a<=b&&b<=c&&a+b>c)m_set.insert(a*12345+b*1234+c);
            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]);
        return ;
    }
    int main()
    {
        int i,j,k,T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(i=0;i<n; i++)scanf("%d",&num[i]);
            m_set.clear();
            dfs(0,0,0,0);
            printf("%d
    ",m_set.size());
        }
        return 0;
    }
    
    //解法2
    
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<set>
    using namespace std;
    typedef pair<int ,int> pair1;
    typedef pair<pair1,int> pair2;
    set<pair2>m_set;
    int n,num[30],sum;
    void dfs(int pos,int a,int b,int c)
    {
        if(pos==n)
        {
            if(a<=b&&b<=c&&a+b>c)m_set.insert(make_pair(make_pair(a,b),c));
            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]);
        return ;
    }
    
    
    
    int main()
    {
        int i,j,k,T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(i=0;i<n; i++)scanf("%d",&num[i]);
            m_set.clear();
            dfs(0,0,0,0);
            printf("%d
    ",m_set.size());
        }
        return 0;
    }
    

  • 相关阅读:
    [KB] Office序列号移除器
    收音机的记忆
    EnCase v7 正式版预览
    关于Ex01和EnCase 6.19的小道消息
    EnCase V7 正式发布 新特性
    [EnCase v7专题] EX01证据文件获取设置释疑
    智能手机应用取证系列之三:腾讯微博Android手机客户端取证分析
    [EnCase v7] EnCase v7零售版改用CodeMeter加密狗
    Http Server的一个示例
    一个简单的加解密算法
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554815.html
Copyright © 2011-2022 走看看