HDU - 4277 USACO ORZ (set判重+dfs)好题

211 篇文章 1 订阅
47 篇文章 1 订阅
HDU - 4277
Time Limit: 1500MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

 Status

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

2012 ACM/ICPC Asia Regional Changchun Online
//题意:输入一个数n,再输入n个数,表示n根木棒的长度
表示给你n根长短不一的木棒,现在让你将这n根木棒都用上,问可以组成几个不相同的三角形(不相同表示至少有一条边的长度不同)。
//思路:
运用dfs将所有的能组成的三角形都找出来,再用set判重,最后得到的即为所求。
#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ll long long
#define N 20
using namespace std;
int A[N];
int SUM;
set<ll>s;
set<ll>::iterator f;
ll ans,n;
void dfs(ll a,ll b,ll c,ll cnt,ll sum)
{
	if(cnt>=n)//所有的边都被加上 
	{
		if(a>b) swap(a,b);
		if(a>c) swap(a,c);
		if(b>c) swap(b,c);
		if(a+b>c)//如果能组成三角形 
		{
			ll h=(ll)a*SUM*SUM+b*SUM+c;//乘上SUM是为了放大边的长度的不同 
			if(s.find(h)==s.end())
			{
				s.insert(h);
				ans++;
			} 
		}
		return ;
	}
	sum-=A[cnt];
	if(a+A[cnt]<b+c+sum&&a+A[cnt]<=b+sum) dfs(a+A[cnt],b,c,cnt+1,sum);//先判断在能组成三角形的条件下再dfs 
	if(b+A[cnt]<a+c+sum&&b+A[cnt]<=c+sum) dfs(a,b+A[cnt],c,cnt+1,sum);
	if(c+A[cnt]<a+b+sum) dfs(a,b,c+A[cnt],cnt+1,sum);
}
int main()
{
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		s.clear();
		scanf("%d",&n);
		SUM=0;
		for(i=0;i<n;i++)
			scanf("%d",&A[i]),SUM+=A[i];
		ans=0;
		dfs(0,0,0,0,SUM);
		printf("%lld\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值