poj1011 _经典搜索

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 133220 Accepted: 31285

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

/**************************************

经典搜索题,剪枝众多。。。上代码

**************************************/

</pre><div><pre name="code" class="cpp">#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <bitset>
using namespace std;

#define INT_MAX 1 << 30

typedef long long ll;
int n;
int sum;
int a[100];
int used [100];
int l;


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

int dfs(int num,int length)                        //剩下的木棍数和拼接需要的棍子长度
{
    if(num == 0 && length == 0)     return 1;    //搜索结束
    if(length == 0)        length = l;                //搜索的继续
    for (int i = 0; i < n; i += 1)
    {
        if(!used[i]&&(a[i] <= length))          //木棒的长度必须小于需要的长度
        {
            if (i > 0)                          //排序后相等的相邻,如果i失败了相等的i-1跳过,剪枝4
            {
                if(!used[i-1]&&(a[i] == a[i-1]))
                {
                    continue;
                }
            }
            used[i] = 1;
            if(dfs(num-1,length-a[i]))
                return 1;    
            else 
            {
                used[i] = 0;                    //搜索失败回溯,深搜的鲜明特征
                if(length == a[i])  return 0;   //棍子的最后一根短棒失败,不必找更短棒代替
                if(length == l)     return 0;   //棍子的第一根长棒失败,后面短棒不必再循环
            }
        }
    }
    return 0;
}

int main()
{
    while ((scanf("%d",&n) != EOF) && (n != 0))
    {
        sum = 0;
        memset(a,0,sizeof(a));              //初始化
        for (int i = 0; i < n; i += 1)
        {
            cin >> a[i];
            sum += a[i];
        }
        qsort(a,n,sizeof(a[0]),cmp);      //从大到小进行排序,提高搜索效率,剪枝1
        for (l = a[0]; l <= sum/2; l += 1)//棍子的最短长度不能低于木棒的最长长度,剪枝2
        {
            if (sum%l)                      //非因子直接跳过,剪枝3
                {
                    continue;
                }
            memset(used,0,sizeof(used));  //每次搜索都要初始化
            
            if (dfs(n,l))                  //由于搜索最短木棒,成功后直接跳出
            {
                cout << l << endl;
                break;
            }
        }
        if(l > sum/2)
            cout << sum << endl;
        
    }
    return 0;
}
欢迎指错与讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值