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. 用switch判断月份的练习

    import java.util.Scanner; public class SwitchTest01 { public static void main(String[] args) { Syste ...

  2. 网络断开后重连downloadProvider继续下载问题调试分析

    最近在安卓4.4上遇到一个断开wifi后重新连接wifi, downloadProvider继续下载文件失败的问题.于是开始了解下载管理模块的断点续载功能:     1.首先,分析android lo ...

  3. Android - Error parsing XML: unbound prefix

    概述 这个问题,虽然看起来不是问题,但是如果不知道的人,还会花点时间,有的人甚至重新安装ADT. 我一开始还以为是排版的问题(Layout),因为初学,弄来弄去,最好还是到网上搜. 其实就不是什么问题 ...

  4. c-100米球反弹

    #include <iostream> #define TIMES 10 int main(void) { ; ; //第一次反弹的高度. ; i <= TIMES; i++) { ...

  5. 杂记之web篇

    问题1:通过POST方式提交给后台的数据出现了乱码,用部分浏览器测试却是好的. 解决办法: 在web.config文件中加上 <globalization responseEncoding=&q ...

  6. 沙盒操作的核心函数 - NSSearchPathForDirectoriesInDomains用法

    1. iPhone会为每一个应用程序生成一个私有目录,这个目录位于: /Users/sundfsun2009/Library/Application Support/iPhone Simulator/ ...

  7. CListBox控件基本功能

    创建CListBox对象 CListBox m_ListBox;关联控件 ,同时注意行数从  0  开始计算 1.向控件中添加内容 int AddString(LPCTSTR lpszItem ); ...

  8. rtmpdump代码分析 转

    RTMPdump 源代码分析 1: main()函数 rtmpdump 是一个用来处理 RTMP 流媒体的工具包,支持 rtmp://, rtmpt://, rtmpe://, rtmpte://, ...

  9. linux笔记2.20

    用户相关:  /etc/passwd  用户信息 /etc/shadow  密码信息 /etc/group  组信息 添加用户:   useradd   -u -g 修改用户: usermod   - ...

  10. Memcached 分布式缓存系统部署与调试

    Memcache 分布式缓存系统部署与调试 工作机制:通过在内存中开辟一块区域来维持一个大的hash表来加快页面访问速度,和数据库是独立的;目前主要用来缓存数据库的数据;存放在内存的数据通过LRU算法 ...