首页 > 搜索 > DFS搜索 > HDU 4277-USACO ORZ-DFS-[解题报告]HOJ
2015
05-23

HDU 4277-USACO ORZ-DFS-[解题报告]HOJ

USACO ORZ

问题描述 :

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.

输入:

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)

输出:

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)

样例输入:

1
3
2 3 4

样例输出:

1

USACO ORZ

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4277

题意:给你n个有长度的线段,问如果用上所有的线段来拼1个三角形,最多能拼出多少种不同的?

题解:暴力搜索,可以用set判重。开始搜索时也可以先把第1个线段固定在一个边上,因为它放哪个边对后面搜索都是一样的。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<string>
#define LL long long
#define N 25
using namespace std;
int num[N];
int n,summ,xj,xk;
int Case;
struct node
{
    int x,y,z;
    bool operator < (const struct node &a) const
    {
		return (x!=a.x)?(x<a.x):(y!=a.y?(y<a.y):(z<a.z));
    }
}temp,ans;
set<struct node> hashx;
void dfs(int);
int main()
{
    scanf("%d",&Case);
    for(int c=0;c<Case;c++)
    {
        summ=0;hashx.clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			summ+=num[i];
		}
		temp.x=num[1];temp.y=0;
		if(n<3)
		{
			printf("%d\n",0);
			continue;
		}
		dfs(2);
        printf("%d\n",hashx.size());
    }
    return 0;
}
void dfs(int x)
{
	if(x>n)
	{
		        xj=temp.x;
				xk=temp.y;
                if(((xj+xk)>(summ-xj-xk))&&(summ-xk>xk)&&(summ-xj>xj))
                {
                    ans.x=max(xj,max(summ-xj-xk,xk));
                    ans.z=min(xj,min(summ-xj-xk,xk));
                    if(xj!=ans.x&&xj!=ans.z) ans.y=xj;
                    else if(xk!=ans.x&&xk!=ans.z) ans.y=xk;
                    else ans.y=summ-ans.x-ans.z;
                    hashx.insert(ans);
                }
				return;
	}
	dfs(x+1);
	temp.x+=num[x];
	dfs(x+1);
	temp.x-=num[x];
	temp.y+=num[x];
	dfs(x+1);
	temp.y-=num[x];
}

来源:http://blog.csdn.net/acm_ted/article/details/7960801

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/acm_ted/article/details/7960801