[关闭]
@Chilling 2017-01-16T16:16:11.000000Z 字数 1552 阅读 956

HDU-4277: USACO ORZ

搜索


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 

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<set>
  4. using namespace std;
  5. int flag,n,a[22];
  6. set< pair<int,int> >s;
  7. void dfs(int l1,int l2,int l3,int count)//分别表示三条边和已经使用了的边数
  8. {
  9. if(count==n)
  10. {
  11. if(l1<=l2&&l2<=l3&&l1+l2>l3)//令短边是l1和l2,之后装入两个短边的值 ,保证他们的顺序,能保证不重复
  12. {
  13. s.insert(pair<int,int>(l1,l2)); //set里不会有重复的数据,装入两个值,第三边也是确定的!
  14. }
  15. return;
  16. }
  17. dfs(l1+a[count],l2,l3,count+1);
  18. dfs(l1,l2+a[count],l3,count+1);
  19. dfs(l1,l2,l3+a[count],count+1);
  20. }
  21. int main()
  22. {
  23. int t,i;
  24. scanf("%d",&t);
  25. while(t--)
  26. {
  27. s.clear();//记住要清空啊!!
  28. memset(a,0,sizeof(a));
  29. flag=1;
  30. scanf("%d",&n);
  31. for(i=0;i<n;i++)
  32. scanf("%d",&a[i]);
  33. if(n<=2)
  34. flag=0;
  35. else
  36. dfs(0,0,0,0);
  37. if(flag==0)
  38. printf("0\n");
  39. else
  40. printf("%d\n",s.size());
  41. }
  42. return 0;
  43. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注