hdu 4277 USACO ORZ(dfs+剪枝)
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 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)
3
2 3 4
题意:给出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+剪枝)的更多相关文章
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 4277 USACO ORZ (dfs暴搜+hash)
题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...
- hdu 4277 USACO ORZ (Dfs)
题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...
- hdu 4277 USACO ORZ
没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- linux下U盘的读取
1.虚拟机vmware右下角,找到大容量存储设备图标,右键->connect(disconect from host):使U盘连接到虚拟机中来. 2.打开终端:fdisk -l [root@lo ...
- ZOJ1524
题意:给定需要购买物品的顺序以及总物品对应的价格,求解按顺序购买物品时最小花费. 输入: m,n(m代表需要购买物品的清单,n代表总的物品数) Xi...(代表对应物品的序号以及价格) 输出: cos ...
- ajax请求在ie8下缓存问题
我今天在改项目bug的时候,发现ajax请求在ie8下有缓存,在缓存过期之前,针对相同地址发起的多个Ajax请求,只有第一次会真正发送到服务端.在某些情况下,这种默认的缓存机制并不是我们希望的(比如获 ...
- Ecstore获取dbschema内容?
有时候在使用dbschema的时候,需要获取dbschema的结构.例如: 那么,我们可以这样写: 这样我就能获得 称呼 这个数组
- C#基础学习第一天(.net菜鸟的成长之路-零基础到精通)
1.Net平台和C#编程语言的概念 2.桌面应用程序: 我们要使用桌面应用程序,必须要安装该应用程序的客户端. winform应用程序. Application:应用程序 Internet:互联网应用 ...
- C#中从元数据
元数据相对我们来说通俗点 就是你引用里面引用的那些dll比如 对Thread 按F12 不就是提示从元数据,..
- STL删除vector或list的方法及注意的问题
删除vector中的元素 1.删除指定的所有对象 STL中remove()只是将待删除元素之后的元素移动到vector的前端,而不是删除.若要真正移除,需要搭配使用erase().例子: vector ...
- OnCreate
用于创建插入符 /* CClientDC dc(this); TEXTMETRIC tm; dc.GetTextMetrics(&tm);//得到窗口字体信息 CreateSolidCaret ...
- JavaScript中的类式继承和原型式继承
最近在看<JavaScript设计模式>这本书,虽然内容比较晦涩,但是细品才发现此书内容的强大.刚看完第四章--继承,来做下笔记. 书中介绍了三种继承方式,类式继承.原型式继承和掺元类继承 ...
- DEIVER_OBJECT结构参数
typedef struct { PDEVICE_OBJECT DeviceObject; //指向驱动程序创建的设备对象 PUNICODE_STRING HardwareDatabase; //记录 ...