C - USACO ORZ

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

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值