HDOJ 4277 USACO ORZ 搜索+剪枝

暴力搜索+轻微剪枝
固定第一个数的话可以减少2/3的时间…..

USACO ORZ

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

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
2012 ACM/ICPC Asia Regional Changchun Online

/* ***********************************************
Author        :CKboss
Created Time  :2015年09月07日 星期一 21时39分52秒
File Name     :HDOJ4277.cpp
************************************************ */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int n,sum;
int a[20];
int seg[3];
int tmp[3];
set<int> st;

void check()
{
    for(int i=0;i<3;i++) tmp[i]=seg[i];
    sort(tmp,tmp+3);

    if(tmp[0]+tmp[1]>tmp[2]) 
        st.insert(tmp[0]*160000+tmp[1]);
    //if(st.count(tmp[0]*300000+tmp[1])==0) cout<

}

void dfs(int x)
{
    if(x==n)
    {
        check();
        return ;
    }
    for(int i=0;i<3;i++)
    {
        seg[i]+=a[x];
        if(seg[i]*21);
        seg[i]-=a[x];
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d",&n);
        sum=0;
        for(int i=0;iscanf("%d",a+i);
            sum+=a[i];
        }
        sort(a,a+n);
        memset(seg,0,sizeof(seg));
        st.clear();
        seg[0]=a[0];
        dfs(1);
        cout<return 0;
}

你可能感兴趣的:(搜索,想法技巧)