Time Limit: 1500MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
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 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
Sample Input
1 3 2 3 4
Sample Output
1
题目:给定一些一定长度的线段,要求全部利用这些线段能拼成多少种三角形(如果两个三角形至少有一条边长度不等那么二者视为两种)
思路:DFS,对于所有满足情况的三角形加入set中,这样就去重了,最后set的大小就是答案
代码:
#include<iostream>
#include<set>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
int a[100],n,m,b[100];
set< pair<int ,int> >s;
void dfs(int x)
{
if(x==m)
{
if(b[0]<=b[1]&&b[1]<=b[2]&&b[0]+b[1]>b[2])
{
s.insert(make_pair(b[0],b[1]));
// cout<<b[0]<<b[1]<<b[2]<<endl;
}
return ;
}
for(int i=0;i<3;i++)
{
b[i]+=a[x];
dfs(x+1);
b[i]-=a[x];
}
}
int main()
{
cin>>n;
while(n--)
{
s.clear();
cin>>m;
for(int i=0;i<m;i++)
cin>>a[i];
dfs(0);
cout<<s.size()<<endl;
}
return 0;
}