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
 
Source
 

题意:给出n条边,求围成三角行有多少种方法

直接dfs,枚举两条边x、y,并且要控制x<y,则第三边由总的减掉就好了,并且用set来标记这个三角形(使用x和y就可以了),以免重复计算

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define ll long long
#define N 16
int n;
int a[N];
set<ll>s;
int sum;
void dfs(int x,int y,int num)
{
int z=sum-x-y;
//if(!(x<=y && y<=z))
//return;
if(num>=n)
{
if(x<=y && y<=z && x+y>z)
{
s.insert(x*10000+y);
}
return;
}
dfs(x+a[num],y,num+1);
dfs(x,y+a[num],num+1);
dfs(x,y,num+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
s.clear();
scanf("%d",&n);
sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
dfs(0,0,0);
printf("%d\n",s.size());
}
return 0;
}

另一种基本上一样,用map来标记

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
#include<map>
using namespace std;
#define N 16
int n;
int z[N];
int vis[N][N][N];
int ans;
int sum;
map<int,int>mp;
void dfs(int a,int b,int c,int num)
{
int res=sum-a-b-c;
if(a>b+c+res)
return; if(b>c+res)
return;
if(num>=n)
{
if(a>b || a>c) return;
if(b>c) return;
if(a+b>c && a+c>b && b+c>a && !mp[a*1000000+b*10+c])
{
ans++;
mp[a*1000000+b*10+c]=1;
} return;
}
dfs(a+z[num],b,c,num+1);
dfs(a,b+z[num],c,num+1);
dfs(a,b,c+z[num],num+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
mp.clear();
sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&z[i]);
sum+=z[i];
}
ans=0;
//memset(vis,0,sizeof(vis));
dfs(0,0,0,0);
printf("%d\n",ans);
}
return 0;
}

hdu 4277 USACO ORZ(dfs+剪枝)的更多相关文章

  1. hdu 4277 USACO ORZ dfs+hash

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  2. hdu 4277 USACO ORZ DFS

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  4. HDU 4277 USACO ORZ(暴力+双向枚举)

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. hdu 4277 USACO ORZ (dfs暴搜+hash)

    题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...

  6. hdu 4277 USACO ORZ (Dfs)

    题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...

  7. hdu 4277 USACO ORZ

    没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  8. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. linux定时任务1-crontab命令

    简单测试例子: 添加定时任务前,注意查看crond服务是否已经启动,如果未启动,则用命令service crond start命令启动. 注意给脚本添加可执行权限. [root@rheltest1 ~ ...

  2. Topcoder SRM 639 (Div.2)

    A.ElectronicPetEasy [题意]一个数st1开始,每次加p1,一共加t1次,另外一个数st2开始,每次加p2,一共加t2次,输入的数均小于1000,问这两个数有没有可能相等,有可能输出 ...

  3. tortoisegit 保存用户名密码

    方法一当你配置好git后,在C:\Documents and Settings\Administrator\ 目录下有一个 .gitconfig 的文件,里面会有你先前配好的name 和email,只 ...

  4. webform repeater

    repeater:由模板构成,解析后模板就不存在了             需要指定数据源进行数据绑定 List<Fruit> list = new FruitDA().Select(); ...

  5. 刚接触js感觉好吃力啊

    我是一个新手,最近刚刚开始学习js这门语言,感觉好难,有一种无从下手的感觉,不知道应该从哪里学习,虽然也看了很多的书,但是对于一个没有计算机基础的人来说,真的是一种煎熬,每一个名词都要去查.万事开头难 ...

  6. php 读xml的两种方式

    <?xml version="1.0" encoding="ISO-8859-1"?> <st> <stu> <nam ...

  7. 获取EnterpriseLibrary企业库配置文件中ConnectionStrings(原创)

    在使用企业类库时想取出单独企业配置文件中的连接字符串遍历,并放到自己的数据库处理类中,在查找了很久的资料后都没有找到,于是自己探索着写了一个,共享给大家以做参考: ConfigurationSourc ...

  8. asp.net微信开发第三篇----自定义会话管理

    和微信用户的沟通少不了,总觉得看起来微信官网后台管理中的会话回复消息有点呆板,所以我这里就自定义了一个会话管理功能,最终效果图如下: 因为我试使用富文本文件CKEDITOR来进行编写,你看到稳中可能会 ...

  9. ViewState探索

    什么是 view state? View State是客户端状态管理重要机制之一.当页面PostBack(向服务器发送或获得数据)时,它能存储页面的值.ASP.NET把View State属性作为页面 ...

  10. php防止重复提交问题

    php防止重复提交问题 用户提交表单时可能因为网速的原因,或者网页被恶意刷新,致使同一条记录重复插入到数据库中,这是一个比较棘手的问题.我们可以从客户端和服务器端一起着手,设法避免同一表单的重复提交. ...