HDU4277 USACO ORZ(dfs+set)

时间:2023-03-09 08:30:52
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来去重。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include<cmath>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <stack>
#include <cctype>
using namespace std;
typedef unsigned long long ull;
#define INF 0xfffffff int l[],n,e[];
set<int> s; //a,b,c是边,k是已经用的边数
void dfs(int a,int b,int c,int k)
{
if(n==k)
{
if((a<=b)&&(b<=c)&&(b+a>c))
{
s.insert(a*+b*+c); //这里为了保证一种形状的唯一性,必须要乘以足够大的数,当b*123时就WA。
} return;
}
dfs(a+l[k],b,c,k+);
dfs(a,b+l[k],c,k+);
dfs(a,b,c+l[k],k+);
return;
} int main()
{
int k,m,q,p;
int T;
cin>>T;
while(T--)
{
s.clear();
memset(l,,sizeof(l));
cin>>n;
for(int i=;i<n;++i)
{
cin>>l[i];
} dfs(,,,); //一定要从0开始,不能dfs(l[0],l[1],l[2],3),这样就没有这三条边组合在一条边上的情况。 cout<<s.size()<<endl; }
return ;
}