POJ 1011 Sticks【深搜+剪枝】

Sticks
Time Limit: 1000MS Memory Limit: 10000K 
Total Submissions: 152276 Accepted: 36300
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

5 2 1 5 2 1 5 2 1 

1 2 3 4 
0

Sample Output

5

题意概括:
  有n根木棒,将其拼成若干根长度相同的木棍,求拼成的木棍的最短长度

       总会得出n根长度相同的木头,因此,一定能整除sum,sum%num==0,因此从大到小进行排序,从最大的开始到sum,判断那个数值可以满足。之后进行递归运算,dfs(num,count,l,pos)num是sum%num==0,count是有多少个sum/num,l是累加的长度,pos是从数组的那个位置开始计算。

当if(len==(stick[i].lenth+l))时,为得出一个成功组合的木棍,count++;同时,l 和 pos也从头开始寻找可能性,即 i=pos=0;。

当(len>(stick[i].lenth+l))时,是为一种可能性,继续向下面寻找可能性。l=l+length;pos=i+1;

 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int n,cnt,sum;//设置全局变量,n表示木棍的数量,sum表示n个木棍的总长度,cnt=sum/i;

struct node
{
    int lenth; // 木棍的长度
    int mark; //标记这根木棍是否被用过;
} stick[66];

int cmp(node a,node b) //按长度从大到小排序
{
    return a.lenth>b.lenth;
}

//len为当前木棒的长度,count统计当前长度木棒的数量,
int dfs(int len,int count,int l,int pos)
{
    if(len==sum)
        return 1; //递归出口
    if(count==cnt)
        return 1;

    for(int i=pos; i<n; i++)
    {
        if(stick[i].mark)
            continue; //如果木棍被标记过,跳过去
        if(len==(stick[i].lenth+l))
        {
            stick[i].mark=1;

            if(dfs(len,count+1,0,0))
                return 1;

            stick[i].mark=0;//上面走不下去了,说明整条路都是死的
            return 0;
        }
        else if(len>(stick[i].lenth+l))
        {
            stick[i].mark=1;
            l+=stick[i].lenth;

            if( dfs(len,count,l,i+1) )
                return 1;

            l-=stick[i].lenth;  //如果不能拼接,那么就恢复
            stick[i].mark=0;

            //上面走不下去了,还可以走别的路
            if(l==0)     //但是l==0,说明了已经是退回到最开始的位置了,说明整条路都是死的
                return 0;

            while(stick[i].lenth==stick[i+1].lenth)//上面走不下去了,说明与i相同的值都走不下去
                i++;  //如果不剪支跑一遍要140MS,加上剪支跑一遍只要31MS,ORZ
        }
    }
    return 0;
}

int main()
{
    while(cin>>n&&n)
    {
        cnt=sum=0;
        for(int i=0; i<n; i++)
        {
            cin>>stick[i].lenth;
            sum+=stick[i].lenth;
            stick[i].mark=0;
        }
        sort(stick,stick+n,cmp);
        for(int i=stick[0].lenth; i<=sum; i++)
        {
            if( sum%i == 0 )
            {
                cnt=sum/i;
                if(dfs(i,0,0,0))
                {
                    printf("%d\n",i);
                    break;
                }
            }

        }

    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值