zoukankan      html  css  js  c++  java
  • hdu 1145(Sticks) DFS剪枝

    Sticks

    Problem Description
    George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. 
     
    Input
    The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
     
    Output
    The output file contains the smallest possible length of original sticks, one per line. 
     
    Sample Input
    9
    5 2 1 5 2 1 5 2 1
    4
    1 2 3 4
    0
     
     
    Sample Output
    6
    5
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    const int N = 100; 
    int n;
    int s[N];
    int v[N];
    int t, length;  //个数  长度 
    bool cmp(int a, int b){
        return a > b;
    }
    bool dfs(int k, int len, int pos){  //第k根  第k跟长度  下标  
        
        //程序出口 
        if(k == t) return true; // 进来的时候肯定是  k = t, len = 0, pos = ? 最后一根的长度肯定是刚好等于  length  !!
        
        if(len == length) return dfs(k+1, 0, 0);  //拼接下一根 
        
        for(int i = pos+1; i <= n; i++){
            if(!v[i] && len+s[i] <= length){
                v[i] = 1;
                if(dfs(k, len+s[i], i)) return true; //这种情况可以 下面的就不用看了 
                v[i] = 0;
                
                // len+s[i] 这种情况不行  
                if(len == 0) return false;  //拼接第 k 跟木棒的 第一节的时候 不能拼成 length   return false 
                while(s[i] == s[i+1]){  //相同的就不用看了 
                    i++;
                }
            }
        } 
        return false;
    }
    int main(){
        while(~scanf("%d", &n) && n){
            int sum = 0;
            for(int i = 1; i <= n; i++){
                scanf("%d", &s[i]);
                sum += s[i];
            }
            sort(s+1, s+1+n, cmp);  //降序
            for(int i = s[1]; i <= sum; i++){
                if(sum%i == 0){
                    memset(v, 0, sizeof v);
                    length = i;  
                    t = sum / i; //组成的木棒个数
                    if(dfs(1, 0, 0)){//当前木棒数, 当前木棒数的长度, 下标 
                        printf("%d
    ", i);
                        break;
                    }
                }
            }
        }    
        return 0;
    }
  • 相关阅读:
    显著提升程序员身心健康和工作效率的装备有哪些?
    谁控制了我们的浏览器?
    利用Http Authentication Url+csrf劫持路由器DNS
    如​何​删​除​G​P​T​保​护​分​区
    Linux定时器的使用
    缺少 mcrypt 扩展。请检查 PHP 配置。(phpmyadmin)
    xxx is not in the sudoers file
    linux mysql 找不到 <mysql/mysql.h>
    C++面试常见问题(持续更新)
    代码模板(new)
  • 原文地址:https://www.cnblogs.com/DDiamondd/p/10857605.html
Copyright © 2011-2022 走看看