程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu4277 dfs+set判重

hdu4277 dfs+set判重

編輯:C++入門知識

hdu4277 dfs+set判重


 

 

Problem 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
/**
hdu4277  dfs+set判重
題目大意:給定一些一定長度的線段,要求全部利用這些線段能拼成多少種三角形(如果兩個三角形至少有一條邊長度不等那麼二者視為兩種)
解題思路:O(3^n) dfs,對於所有滿足情況的三角形加入set中,這樣就去重了,最後set的大小就是答案
*/
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;

int n,ans,a[20];
set s;

void dfs(int cnt,int x,int y)
{
    int z=ans-x-y;
    if(cnt==n)
    {
        if(x<=y&&y<=z&&x+y>z)
        {
            s.insert(x*10000000+y);
        }
        return;
    }
    dfs(cnt+1,x+a[cnt],y);
    dfs(cnt+1,x,y+a[cnt]);
    dfs(cnt+1,x,y);
}
int main()
{
    int T;
    scanf(%d,&T);
    while(T--)
    {
        ans=0;
        s.clear();
        scanf(%d,&n);
        for(int i=0; i

 

Problem 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved