• HDU - 5937 E - Equation(搜索)


    Little Ruins is a studious boy, recently he learned addition operation! He was rewarded some number bricks of 11 to 99 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 = 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 i^{th}ith integer indicates the number of bricks of ii.

    Limits
    1 \leq T \leq 301≤T≤30
    0 \leq \text{bricks number of each type} \leq 1000≤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

    InputcopyOutputcopy
     
    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
     
    Case #1: 2 Case #2: 6 Case #3: 2

    题意:给你1~9的数的个数,让你求形如x+y=z的柿子的个数(x,y,z都是个位数,任意一个数不同就算是一个不同的柿子)

    思路:打表一下可以知道,最终不同的柿子只有36个

    然后用结构体记下每个柿子的数还有他们的总数

    如果读入的数超过了这些数的个数,那么直接输出36就行

    然后我们再进行dfs,找每个柿子能不能选就行了

    1. /*
    2. .----------------. .----------------. .----------------. .----------------.
    3. | .--------------. || .--------------. || .--------------. || .--------------. |
    4. | | ________ | || | _________ | || | ____ ____ | || | ____ | |
    5. | | |_ ___ `. | || | |_ ___ | | || ||_ \ / _|| || | .' `. | |
    6. | | | | `. \ | || | | |_ \_| | || | | \/ | | || | / .--. \ | |
    7. | | | | | | | || | | _| _ | || | | |\ /| | | || | | | | | | |
    8. | | _| |___.' / | || | _| |___/ | | || | _| |_\/_| |_ | || | \ `--' / | |
    9. | | |________.' | || | |_________| | || ||_____||_____|| || | `.____.' | |
    10. | | | || | | || | | || | | |
    11. | '--------------' || '--------------' || '--------------' || '--------------' |
    12. '----------------' '----------------' '----------------' '----------------'
    13. */
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include
    24. #define int long long
    25. #define lowbit(x) x&(-x)
    26. #define PI 3.1415926535
    27. #define endl "\n"
    28. using namespace std;
    29. typedef long long ll;
    30. typedef pair<int,int> pii;
    31. int gcd(int a,int b) {
    32. return b? gcd(b,a%b):a;
    33. }
    34. /*
    35. int dx[8]={-2,-2,-1,1,2,2,-1,1};
    36. int dy[8]={-1,1,2,2,1,-1,-2,-2};
    37. int dx[4]={0,-1,0,1};
    38. int dy[4]={-1,0,1,0};
    39. int dx[8]={-1,1,0,0,-1,-1,1,1};
    40. int dy[8]={0,0,-1,1,-1,1,-1,1};
    41. */
    42. //int e[N],ne[N],h[N],idx,w[N];
    43. /*void add(int a,int b,int c){
    44. e[idx]=b;
    45. w[idx]=c;
    46. ne[idx]=h[a];
    47. h[a]=idx++;
    48. }
    49. */
    50. const int N=2e5+10;
    51. int n;
    52. int cnt[12];
    53. int num[12];
    54. int ans;
    55. struct name{
    56. int x,y,z;
    57. }q[40];
    58. void init(){//记录每个柿子和每个数用了几次
    59. int op=0;
    60. for(int i=1;i<=9;i++){
    61. for(int j=1;j<=9;j++){
    62. if(i<=9&&j<=9&&i+j<=9){
    63. cnt[i]++;
    64. cnt[j]++;
    65. cnt[i+j]++;
    66. q[++op]={i,j,i+j};
    67. }
    68. }
    69. }
    70. }
    71. void dfs(int i,int sum){//到第i个柿子选了sum个柿子
    72. ans=max(sum,ans);
    73. if(i>36){//如果大于36的话我们就剪枝
    74. return ;
    75. }
    76. if(sum+36-i+1<=ans) return ;//这步是最大值剪枝,当我们选了剩下的所有的柿子之后没超过当前的最大值,那么我们直接返回
    77. int x=q[i].x ,y=q[i].y ,z=q[i].z ;
    78. //选第i个柿子
    79. if(num[x]>0&&num[y]>0&&num[z]>0){//如果当前柿子的个数>0
    80. num[x]--;
    81. num[y]--;
    82. num[z]--;
    83. if(num[x]>=0 && num[y]>=0 && num[z]>=0)dfs(i+1,sum+1);//如果选了之后他们的个数不是负数,就进入下一个柿子,总数++
    84. num[x]++;
    85. num[y]++;
    86. num[z]++;//回溯
    87. }
    88. //不选第i个柿子
    89. dfs(i+1,sum);
    90. }
    91. signed main(){
    92. int t;
    93. init();
    94. scanf("%d",&t);
    95. for(int p=1;p<=t;p++){
    96. for(int i=1;i<=9;i++)scanf("%d",&num[i]);
    97. bool f=true;
    98. for(int i=1;i<=9;i++){
    99. if(num[i]
    100. f=false;
    101. break;
    102. }
    103. }
    104. if(f){
    105. printf("Case #%d: %d\n",p,36);
    106. continue;
    107. }
    108. ans=0;
    109. dfs(1,0); //从第一个柿子开始,选了0个柿子
    110. printf("Case #%d: %d\n",p,ans);
    111. }
    112. return 0;
    113. }

  • 相关阅读:
    vscode debug时 stepinto 无法跳进代码
    go的日志库logrus
    oracle 打补丁
    【重学Java四】Object通用方法、继承
    Tomcat
    字节跳动八进八出,offer到手,发现项目不重要算法才最重要
    OPENCV--Haar+Tesseract车牌识别;
    2022/08/26 day11:高级数据类型
    yolov3学习笔记
    『 MySQL数据库 』数据库之表的约束
  • 原文地址:https://blog.csdn.net/qq_61903556/article/details/127566571