HDU 4277 USACO ORZ 第37届ACM/ICPC长春赛区网络赛1011题(搜索)

USACO ORZ

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

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  

 

Recommend liuyiding      
就是搜索,在长春网络赛上做得还是比较顺畅的题目了。  
做搜索题目。。。还是稍微好点了。  
我用map判重的,而且要注意几个地方要剪枝,否则会TLE的。  

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=20;
int a[MAXN];
map <long long,int> mp;
int tol;
int n;
int sum;
void dfs(int l1,int l2,int l3,int i)
{
    if(l1>l2)swap(l1,l2);
    if(l1>l3)swap(l1,l3);
    if(l2>l3)swap(l2,l3);
    if(l3*2>sum)return;
    if(i==n)
    {
        if(l1+l2>l3)
        {
            long long t=l1*150000*150000+l2*150000+l3;
            if(mp[t]==0)
            {
                mp[t]=1;
                tol++;
            }
        }
        return;
    }
    int t11=l1+a[i+1];
    int t12=l2;
    int t13=l3;
    if(t11>t12)swap(t11,t12);
    if(t11>t13)swap(t11,t13);
    if(t12>t13)swap(t12,t13);
    dfs(t11,t12,t13,i+1);
    int t21=l1;
    int t22=l2+a[i+1];
    int t23=l3;
    if(t21>t22)swap(t21,t22);
    if(t21>t23)swap(t21,t23);
    if(t22>t23)swap(t22,t23);
    if(t11!=t22 || t12!=t22 ||t13!=t23)dfs(t21,t22,t23,i+1);
    int t31=l1;
    int t32=l2;
    int t33=l3+a[i+1];
    if(t31>t32)swap(t31,t32);
    if(t31>t33)swap(t31,t33);
    if(t32>t33)swap(t32,t33);
    if(t31!=t11||t32!=t12||t33!=t13)
      if(t31!=t21||t32!=t22||t33!=t23)
         dfs(t31,t32,t33,i+1);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        sum=0;
        for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];}
        mp.clear();
        tol=0;
        dfs(0,0,0,0);
        printf("%d\n",tol);
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/09/10/2679034.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞