HDU4277 USACO ORZ(dfs+set)

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

刚看到的时候以为是排列组合一类的问题,看了题解以后才看出其简单的解法,也是暴力解法,题目就是这种目的。。。每种情况挨个试,用set来去重。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cctype>
 6 #include <cstdlib>
 7 #include<cmath>
 8 #include <string>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <vector>
13 #include <stack>
14 #include <cctype>
15 using namespace std;
16 typedef unsigned long long ull;
17 #define INF 0xfffffff
18 
19 int l[16],n,e[3];
20 set<int> s;
21 
22 //a,b,c是边,k是已经用的边数 
23 void dfs(int a,int b,int c,int k)
24 {
25     if(n==k)
26     {
27         if((a<=b)&&(b<=c)&&(b+a>c))
28         {
29             s.insert(a*12345+b*1234+c);        //这里为了保证一种形状的唯一性,必须要乘以足够大的数,当b*123时就WA。 
30         }
31         
32         return;
33     }
34     dfs(a+l[k],b,c,k+1);
35     dfs(a,b+l[k],c,k+1);
36     dfs(a,b,c+l[k],k+1);
37     return;
38 }
39 
40 int main()
41 {
42     int k,m,q,p;
43     int T;
44     cin>>T;
45     while(T--)
46     {
47         s.clear();
48         memset(l,0,sizeof(l));
49         cin>>n;
50         for(int i=0;i<n;++i)
51         {
52             cin>>l[i];
53         }
54         
55         dfs(0,0,0,0);    //一定要从0开始,不能dfs(l[0],l[1],l[2],3),这样就没有这三条边组合在一条边上的情况。 
56         
57         cout<<s.size()<<endl;
58 
59     }
60     return 0;
61 }

 

posted @ 2015-10-11 16:06  Traveller_Leon  阅读(235)  评论(0编辑  收藏  举报