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

分析

这个题还是比较有意思的,多根木棒要拼成若干根等长的木棍,用深搜搜索所有可能性,可采取多种剪枝方法,讲义已讲的很全,相关可剪枝原因已在代码中加上注释。

实现

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
vector<int> len(65);
//木棒是否已使用过
int used[65];
int L;
int n;

/*
*剩余rest根木棒时,当前棍子还差期望长度L的值为needLen时是否有搜索结果
*/
bool dfs(int rest, int needLen, int lastIndex)
{
    if (rest == 0 && needLen == 0) {
        return true;
    }
    //一根拼完拼新的一根,直到rest为0。
    if (needLen == 0) {
        needLen = L;
    }
    //剪枝4:多根木棒组成一根木棍,让木棒按长度由大到小排列组成,不必搜索当前木棒比上一根木棒长的情况,因为如果此种情况可以的话
    //只是相当于将组成情况不按长度大小排序,不按长度大小排序可成功那么按长度大小排序也可成功。
    int startInex = needLen == L ? 0 : lastIndex + 1;
    for(int i = startInex; i < n; i++) {
        //剪枝1:上次没拼成功,这次不尝试相同长度的木棒。
        if (i > 0 && !used[i-1] && len[i-1] == len[i]) {
            continue;
        }
        if (!used[i] && len[i] <= needLen) {
            used[i] = 1;
            if (dfs(rest - 1, needLen - len[i], i)) {
                return true;
            }else {
                //没拼成功,换下一根。
                used[i] = 0;
                //剪枝2:len[i]为第一根木棒的情况下没拼成功,则这根木棒永远不能再被用上,所以不可能全部木棒被使用,
                //后续搜索全部放弃。
                if (needLen == L
                    //剪枝3:假设len[i]为最后一根木棒,被更小木棒替换后最终能够成功,那么3必然出现在后面的某个棍子k里。
                    //将棍子k中的len[i]和棍子i中用来替换3的几根木棒对调,结果当然一样是成功的。这就和i原来的拼法会导致不成功矛盾。
                    || needLen == len[i]) {
                    return false;
                }
            }
        }
    }
    return false;
}

int main()
{
//  freopen("in.txt", "r", stdin);
    while (cin >> n && n > 0) {
        len.clear();
        int minLen = 0;
        int totalLen = 0;
        int tempLen;
        for(int i = 0;i<n; i++) {
            cin >> tempLen;
            len.push_back(tempLen);
            totalLen += tempLen;
        }
        //保持棒子长度由大到小
        sort(len.begin(), len.end(), greater<int>());
        for(L = len[0]; L <= totalLen / 2; L++) {
            //能整除才能用上全部木棒
            if (totalLen % L) {
                continue;
            }
            memset(used, 0, sizeof(used));
            if (dfs(n, L, -1)) {
                //找到最小长度即可
                minLen = L;
                break;
            }
        }
        //搜索不到组成2根及2根以上棍子的方法,则所有木棒只能组成一根棍子。
        if (L > totalLen / 2) {
            minLen = totalLen;
        }
        cout << minLen << endl;
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值