hdu 5937 -- Equation(搜索)
2018-06-17 21:50:46来源:未知 阅读 ()
题目链接
problem description
Now little Ruins is puzzled by those bricks because he wants to put those bricks into as many different addition equations form x+y=zx+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, xx, yy and zz will be single digit number.
Two addition equations are different if any number of xx, yy and zz is different.
Please help little Ruins to calculate the maximum number of different addition equations.
Input
First line contains an integer TT, which indicates the number of test cases.
Every test case contains one line with nine integers, the ithith integer indicates the number of bricks of ii.
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
题意:输入c[1]~c[9]分别表示1~9这些数字的个数,现在用这些数字构成等式x+y=z(x,y,z都是个位的数字),等式不能相同(1+2=3与2+1=3不同),求最多能都成多少个等式?
思路:先用结构体数组存储所用的等式,共36个,然后搜索就行。注意剪枝;
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int ans; int c[10]; struct Node { int x,y,z; }a[40]; void init() { int tot=1; for(int i=1;i<=8;i++) { for(int j=1;i+j<=9;j++) { a[tot].x=i; a[tot].y=j; a[tot].z=i+j; tot++; } } } void dfs(int i,int sum) { if(i>=37) { ans=max(ans,sum); return ; } if(sum+36-i+1<=ans) return ; int x=a[i].x; int y=a[i].y; int z=a[i].z; if(c[x]>0&&c[y]>0&&c[z]>0) { c[x]--; c[y]--; c[z]--; if(c[x]>=0&&c[y]>=0&&c[z]>=0) dfs(i+1,sum+1); c[x]++; c[y]++; c[z]++; } dfs(i+1,sum); } int main() { init(); int T,Case=1; cin>>T; while(T--) { for(int i=1;i<=9;i++) scanf("%d",&c[i]); ans=0; dfs(1,0); printf("Case #%d: %d\n",Case++,ans); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:二分答案 ①
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash