码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4277 USACO ORZ(dfs+剪枝)

时间:2015-08-14 21:02:19      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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
 

 题意:给出n条边,求围成三角行有多少种方法

直接dfs,枚举两条边x、y,并且要控制x<y,则第三边由总的减掉就好了,并且用set来标记这个三角形(使用x和y就可以了),以免重复计算

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<set>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<stdlib.h>
 9 #include<map>
10 using namespace std;
11 #define ll long long
12 #define N 16
13 int n;
14 int a[N];
15 set<ll>s;
16 int sum;
17 void dfs(int x,int y,int num)
18 {
19     int z=sum-x-y;
20     //if(!(x<=y && y<=z))
21       //return;
22     if(num>=n)
23     {
24         if(x<=y && y<=z && x+y>z)
25         {
26             s.insert(x*10000+y);
27         }
28         return;
29     }
30     dfs(x+a[num],y,num+1);
31     dfs(x,y+a[num],num+1);
32     dfs(x,y,num+1);
33 }
34 int main()
35 {
36     int t;
37     scanf("%d",&t);
38     while(t--)
39     {
40         s.clear();
41         scanf("%d",&n);
42         sum=0;
43         for(int i=0;i<n;i++)
44         {
45             scanf("%d",&a[i]);
46             sum+=a[i];
47         }
48         dfs(0,0,0);
49         printf("%d\n",s.size());
50     }
51     return 0;
52 } 
View Code

 

 

另一种基本上一样,用map来标记

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<set>
 5 #include<vector>
 6 #include<map>
 7 using namespace std;
 8 #define N 16
 9 int n;
10 int z[N];
11 int vis[N][N][N];
12 int ans;
13 int sum;
14 map<int,int>mp;
15 void dfs(int a,int b,int c,int num)
16 {
17     int res=sum-a-b-c;
18     if(a>b+c+res)
19       return;
20     
21     if(b>c+res)
22        return;
23     if(num>=n)
24     {
25         if(a>b || a>c) return;
26         if(b>c) return;
27         if(a+b>c && a+c>b && b+c>a && !mp[a*1000000+b*10+c])
28         {
29             ans++;
30             mp[a*1000000+b*10+c]=1;
31         }
32           
33         return;
34     }
35     dfs(a+z[num],b,c,num+1);
36     dfs(a,b+z[num],c,num+1);
37     dfs(a,b,c+z[num],num+1);
38 }
39 int main()
40 {
41     int t;
42     scanf("%d",&t);
43     while(t--)
44     {
45         mp.clear();
46         sum=0;
47         scanf("%d",&n);
48         for(int i=0;i<n;i++)
49         {
50             scanf("%d",&z[i]);
51             sum+=z[i];
52         }
53         ans=0;
54         //memset(vis,0,sizeof(vis));
55         dfs(0,0,0,0);
56         printf("%d\n",ans);
57     }
58     return 0;
59 }
View Code

 

hdu 4277 USACO ORZ(dfs+剪枝)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4731076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!