hdoj--4277--USACO ORZ(dfs+剪枝)

来源:互联网 发布:知乎live百度云 编辑:程序博客网 时间:2024/06/05 16:11


USACO ORZ

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


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
132 3 4
 

Sample Output
1
题意:n支木棒,将这n支木棒全部用完可以组成多少个三角形,全部的木棒都用来组成一个三角形,
暴力枚举加剪枝,三角形的一条边肯定是不能比三角形周长的一半大的,并且必须先把第一根木棒分给一个堆,要不然会超时
#include<cstdio>#include<map>#include<cstring>#include<algorithm>using namespace std;int n,num[20],ans;__int64 now[5];__int64 sum;map<__int64,bool>st;bool check(){__int64 a=now[0],b=now[1],c=now[2];if(a>b) swap(a,b);if(a>c) swap(a,c);if(b>c) swap(b,c);if(a&&b&&c&&a+c>b&&a+b>c&&b+c>a){if(!st[a*150000+b]){st[a*150000+b]=true;return true;}}return false;}void dfs(int d){if(d==n){if(check())ans++;return ;}for(int i=0;i<3;i++){now[i]+=num[d];if(now[i]<=sum/2)dfs(d+1);now[i]-=num[d];}}int main(){int t;scanf("%d",&t);while(t--){st.clear();scanf("%d",&n);sum=0,ans=0;memset(num,0,sizeof(num));memset(now,0,sizeof(now));for(int i=0;i<n;i++)scanf("%d",&num[i]),sum+=num[i];now[0]=num[0];dfs(1);printf("%d\n",ans);}return 0;} 

0 0
原创粉丝点击