HDU5937 Equation(DFS + 剪枝)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=5937
Description
Little Ruins is a studious boy, recently he learned addition operation! He was rewarded some number bricks of 1 to 9 and infinity bricks of addition mark '+' and equal mark '='.
Now little Ruins is puzzled by those bricks because he wants to put those bricks into as many different addition equations form x+y=z as possible. Each brick can be used at most once and x, y, z are one digit integer.
As Ruins is a beginer of addition operation, x, y and z will be single digit number.
Two addition equations are different if any number of x, y and z is different.
Please help little Ruins to calculate the maximum number of different addition equations.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case contains one line with nine integers, the ith integer indicates the number of bricks of i.
Limits
1≤T≤30
0≤bricks number of each type≤100
Output
For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result.
Sample Input
3
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
0 3 3 0 3 0 0 0 0
Sample Output
Case #1: 2
Case #2: 6
Case #3: 2
分析
题目大概说有若干个1到9这几个数字问最多能拼成多少种x+y=z的等式?
- x+y=z有36种。由于x>y和x<y是对称的,只考虑x<=y,有20种,16种是x<y,4种x=y。。
- 直接暴力搜索。。对于x<y,可以选1种、选2种和不选;对于x=y可以选和不选。
- 那么这样时间复杂度是$O(3^{16}*2^4)$。。
- 不剪枝会超时的,我加了几个预测的最优性剪枝,利用剩下的各个数字的个数粗略估算最多还能加入几种等式。。
- 实测100 100 100 100 100 100 100 100 100 100秒出= =。。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int x[]={1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4};
int y[]={2,3,4,5,6,7,8,3,4,5,6,7,4,5,6,5};
int z[]={3,4,5,6,7,8,9,5,6,7,8,9,7,8,9,9}; int x2[]={1,2,3,4};
int y2[]={2,4,6,8}; int a[11],ans;
void dfs2(int k,int n){
if(ans<n) ans=n;
if(k==4) return;
if(a[x2[k]]>1 && a[y2[k]]){
a[x2[k]]-=2; --a[y2[k]];
dfs2(k+1,n+1);
a[x2[k]]+=2; ++a[y2[k]];
}
dfs2(k+1,n);
}
inline calc(int a,int b){
if(a<b) return a<<1;
return b<<1;
}
void dfs(int k,int n){
if(k<=7){
if(n+4+calc(a[1],7-k)+calc(a[2],5)+calc(a[3],3)+calc(a[4],1)<ans) return;
}else if(k<=12){
if(n+4+calc(a[2],12-k)+calc(a[3],3)+calc(a[4],1)<ans) return;
}else{
if(n+4+calc(a[3],15-k)+calc(a[4],1)<ans) return;
}
if(k==16){
dfs2(0,n);
return;
}
dfs(k+1,n);
if(((x[k]==y[k]&&a[x[k]]>1) || (x[k]!=y[k]&&a[x[k]])) && a[y[k]] && a[z[k]]){
--a[x[k]]; --a[y[k]]; --a[z[k]];
dfs(k+1,n+1);
++a[x[k]]; ++a[y[k]]; ++a[z[k]];
}
if(x[k]!=y[k] && a[x[k]]>1 && a[y[k]]>1 && a[z[k]]>1){
a[x[k]]-=2; a[y[k]]-=2; a[z[k]]-=2;
dfs(k+1,n+2);
a[x[k]]+=2; a[y[k]]+=2; a[z[k]]+=2;
}
} int main(){
int t;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
for(int i=1; i<=9; ++i){
scanf("%d",a+i);
}
ans=0;
dfs(0,0);
printf("Case #%d: %d\n",cse,ans);
}
return 0;
}
HDU5937 Equation(DFS + 剪枝)的更多相关文章
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
随机推荐
- Oracle EBS R12的启停脚本
以下脚本用root用户登录执行: 一.DB启停使用EBS提供的脚本ebs_start.shsu - oraprod -c "/d01/oracle/PROD/db/tech_st/10.2. ...
- linux磁盘空间查询
LINUX服务器查询 1. du -sch * 使用该命令查询当前目录下文件夹占用的空间的情况 2. df -hl 查询磁盘剩余空间 3. root权限 fdisk -l
- 分分钟教你从根本上认识Struts2框架
在了解Struts2之前我们先来聊聊Struts1,我们都知道在很长的一段时间内,所有的MVC框架中,Struts1他是处于一个超级大咖的地位,无论是从市场角度和使用的用户的数量这个角度而言,Stru ...
- Linux学习之十--.Net Core环境搭建以及Nginx的搭建
一.Centos7下.Net Core 环境安装: 链接:https://www.microsoft.com/net/core#linuxcentos 按照步骤来: yum install libun ...
- HDU5934 强连通分量
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...
- 我的js函数库(持续更新)
常用js初始化函数 function id(obj) { return document.getElementById(obj); } function bind(obj, ev, fn) { if ...
- Visual Studio 常用快捷键备忘
在代码中插入书签 用途 操作 vs2013 快速在自定义的不同代码位置跳转 首先点击: 编辑=>书签=>启用书签 然后再在代码编辑窗口 ctrl+k, k (取消书签,再按一次 ctr ...
- Ajax加载菊花loding效果
Ajax 异步请求的时候,一般都会利用一个动态的 gif小图片来制作一个Ajax Loading ,以便增加用户体验. 这里我们使用Spin.js ,该 js 脚本压缩后5k,可以不用任何图片,任何外 ...
- 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记
使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...
- PSD文件在MAC上和在WINDOWS上的大小有本质区别
因为偷懒在MAC上的美工,发我的PSD文件,我就直接在上面做了= =后来不知道为什么无论我怎么合并图层.PSD的大小永远都是107M....然后忍无可忍重新画就从107M变成2M.....MAC为什么 ...