zoukankan      html  css  js  c++  java
  • HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277

    原题描述:

    USACO ORZ

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

    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个数,要你用光所有数字来构成一个三角形,问:能组成多少种不同的三角形;
    题目思路:DFS深搜一遍,进行不必要的剪枝,然后使用set容器输入,去掉重复的值,最后输出set容器中值的个数。
    AC代码:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <set>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    set<LL>s;
    int num[20];
    int sum;
    int n;
    
    void dfs(int a,int b,int c,int i)
    {
        if(i==n+1)
        {
            if(a<=b&&b<=c)//重要部分保证a<=b<=c
                if(a&&b&&c&&a+b>c)
                    s.insert(a*sum*sum+b*sum+c);//构造一个唯一值避免重复数值出现
            return;
        }
        dfs(a+num[i],b,c,i+1);
        dfs(a,b+num[i],c,i+1);
        dfs(a,b,c+num[i],i+1);
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n;
            for(int i=1; i<=n; i++)
            {
                cin>>num[i];
                sum+=num[i];
            }
            s.clear();
            dfs(0,0,0,1);
            cout<<s.size()<<endl;
        }
        return 0;
    }

      

  • 相关阅读:
    activiti 清库脚本(转)
    T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
    Chrome firefox ie等浏览器空格(&nbsp;)兼容问题
    activiti5.22整合modeler时出错TypeError: Cannot read property 'split' of undefined
    activiti如何获取当前节点以及下一步路径或节点(转)
    Activiti5 添加/查询审批批注(审批意见)
    Django中多对多关系的orm表设计
    css背景雪碧图等
    Django图书管理系统(前端对有外键的数据表增删改查)
    Django图书管理系统(前端对数据库的增删改查)
  • 原文地址:https://www.cnblogs.com/teble/p/7206189.html
Copyright © 2011-2022 走看看