ACM: 一题DFS(深搜,剪枝) poj 1011

 

                                                                              Sticks
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 should 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

题意: 给你n根小木棒,找出能恰好拼凑成最大的长度.
          例如第一个例子: (5,1) , (5,1) , (5,1) , (2,2,2) => 6
                   第二个例子: (2,3) , (1,4) => 5

解题思路:
                1. 一开始想到是动态规划,像背包问题.
                2. 用深搜的思想试一试,木棒长度的尝试法.  len = stick[0] ; len <= sum; len++;
                3. dfs(深搜就要涉及剪枝问题).
                    递归的参数: 尝试长度len , 离目标还需要长度reLen , 木棒剩余条数: num;
       问题分析:
           (1).递归入口: 因为木棒拼凑成的条数是整数,所以一开始可以剪枝一次,sum % len == 0
           (2).确定递归的出口是: 当剩余木棒条数为0时, 并且reLen = 0;
        循环递归中:
           (3).当木棒被访问过continue;
           (4).reLen 必须要大于等于 当前木棒的长度(reLen >= stick[i]);
           (5).在(4)的基础上可以再次剪枝 stick[i] == reLen || reLen == len
           (
              解释: 本来表示即将拼成一根棒,或刚进行新一轮拼凑
                       (即if中判断的是主问题性质完全相同的子问题)
                       但经过上面的if判断,子问题不能完成所有任务,
                       那么整体不可能完成任务,不再考虑,搜索失败(剪枝)
             )
           (6).将相同长度的木棒跳跃(剪枝);(相同不能解决问题的子问题进行剪枝).

代码:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAX 70

int stick[MAX];
bool visited[MAX];

int  n;
int sum;

int cmp(const void *a,const void *b)
{
    return *(int*)b - *(int*)a;
}

int dfs(int len,int reLen,int num)
{
    if(reLen == 0 && num == 0)
        return len;
    if(reLen == 0)
        reLen = len;
       
    for(int i = 0; i < n; ++i)
    {
        if(visited[i] == 1)
            continue;
        if(reLen >= stick[i])
             
            visited[i] = 1;
            if(dfs(len,reLen-stick[i],num-1) != 0)
                return len;
            visited[i] = 0;
            if(stick[i] == reLen || reLen == len)
                break;
            while(stick[i] == stick[i+1])
                i++;
        }
    }
    return 0;
}

int main()
{
//    freopen("input.txt","r",stdin);
    while(scanf("%d",&n) != EOF && n != 0)
    {
        sum = 0;
        for(int i = 0; i < n; ++i)
        {
            scanf("%d",&stick[i]);
            sum += stick[i];
        }
        qsort(stick,n,sizeof(stick[0]),cmp);
        int result;
        for(int len = stick[0]; len <= sum; ++len)
        {
            if(sum % len == 0)
            {
                result = 0;
                memset(visited,0,sizeof(visited));
                result = dfs(len,0,n);
                if(result != 0)
                    break;
            }
        }
        printf("%d\n",result);
    }
   
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值