Like everyyione, 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.
InputThe 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)
OutputFor each test case, output one integer indicating the number of different pastures.
Sample Input
Sample Output
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)
1 3 2 3 4
1
题意:给你N条边,用这N条边组成三角行,问有多少个不同的三角形。这是正的题意,而我在开始时是把他认为在n条边里调选3条边,问可以组成多少不同的三角形;
总之题都没读明白,一切都是浮云啊。
题解:用dfs求得所有情况,set来排除重复的情况;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include<algorithm>
using namespace std;
set<pair<int,int> >s;
int a[110],n,b[4],flag[110],co=0;
void dfs(int t)
{
if(t==n)
{
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[t];dfs(t+1);b[i]-=a[t];//dfs是个岔路口,一个方向加了a[t],另一个没有
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>n;s.clear();
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
dfs(0);
cout<<s.size()<<endl;
}
}

探讨如何使用特定数量和长度的围栏段构建不同类型的三角形牧场。通过深度优先搜索算法找出所有可能的组合,并利用集合数据结构去除重复的解决方案。
417

被折叠的 条评论
为什么被折叠?



