HDU 4277 USACO ORZ(DFS暴搜+set去重)

原题代号:HDU 4277

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277

原题描述:

USACO ORZ

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

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
 
题目大意:给你n个数,要你用光所有数字来构成一个三角形,问:能组成多少种不同的三角形;
题目思路:DFS深搜一遍,进行不必要的剪枝,然后使用set容器输入,去掉重复的值,最后输出set容器中值的个数。
AC代码:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; set<LL>s;
int num[];
int sum;
int n; void dfs(int a,int b,int c,int i)
{
if(i==n+)
{
if(a<=b&&b<=c)//重要部分保证a<=b<=c
if(a&&b&&c&&a+b>c)
s.insert(a*sum*sum+b*sum+c);//构造一个唯一值避免重复数值出现
return;
}
dfs(a+num[i],b,c,i+);
dfs(a,b+num[i],c,i+);
dfs(a,b,c+num[i],i+);
} int main()
{
//freopen("in.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=; i<=n; i++)
{
cin>>num[i];
sum+=num[i];
}
s.clear();
dfs(,,,);
cout<<s.size()<<endl;
}
return ;
}

  

上一篇:破解LR时,解决loadrunner 破解错误:license security violation.Operation is not allowed


下一篇:Jsensation | 氪加