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 ...
随机推荐
- 将用户信息保存到Cookie中
/** * 把用户保存到Cookie * * @param request * @param response * @param member */ private void rememberPwdA ...
- Java多线程——线程的生命周期和状态控制
一.线程的生命周期 线程状态转换图: 1.新建状态 用new关键字和Thread类或其子类建立一个线程对象后,该线程对象就处于新生状态.处于新生状态的线程有自己的内存空间,通过调用start方法进入就 ...
- LINQ Enumerable 续 II
Enumerable.TakeWhile和Enumerable.SkpWhile Enumerable.TakeWhile和Enumerable.SkpWhile将通过判断条件,来获取和跳过序列. T ...
- java.io.FileNotFoundException: class path resource [bean/test/User.hbm.xml] cannot be opened because it does not exist
确定下 WEB-INF/classes下有没有,不是src下哦 工程的src下创建后,会发布到tomcat下项目下的classes中
- 配置mysql允许远程连接
开启MySQL数据库的远程连接权限: use mysql://进入数据库 grant all privileges on *.* to root@'%'identified by '123465789 ...
- 数据库触发器new old
数据库触发器new old: "NEW . column_name"或者"OLD . column_name".这样在技术上处理(NEW | OLD . col ...
- Ubuntu等Linux系统清除DNS缓存的方法
buntu等Linux系统清除DNS缓存的方法 直接说方法: 如果系统下有nscd,那么就直接 sudo /etc/init.d/nscd restart 如果没有也没关系,网上接受的方法大都是 su ...
- 用c#在Access数据库中创建新表
生成表NewTable,该表有文本字段Field1和整型字段Field2 private void CreateNewTable() { OleDbConnection conn = new ...
- Linux_ERROR 1045 (28000): Access denied for user 'root'@'localhost'
MySQL生成了root用户的随机密码(如下截图所示),并将这个随机密码放置在/root/.mysql_secret中.并且强制在第一次登陆时修改root用户的密码.Mysql 5.6及以后版本出处于 ...
- sitecore(key\value\language)的灵活应用
1.当我们在做网站的时候是否会因为一个页面的文字变动来回改变.这样的麻烦sitecore都帮我们解决了. 2.sitecore分类key和value和语言几个维度.不同的key会因为不同的语言显示不同 ...