POJ 1011 Sticks(经典dfs)


Language:
Default简体中文

Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 120720   Accepted: 27951

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

Source

Central Europe 1995

题意:给你紧跟棍子,问可能是哪几根相同的棍子截成的,求可能的棍子的最小值

很难的dfs,剪枝很重要,我也是看别人题解的⊙﹏⊙b汗,解释在代码中

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

#define N 105

int a[N],len,n;
int vis[N],sum;

int cmp(int a,int b)
{
    return a>b;
}

bool dfs(int pos,int temp,int cut)
{
    if(cut==sum/len)  //拼凑成功
        return true;

    int i;

    for(i=pos;i<n;i++)
    {
        if(vis[i]==0&&temp+a[i]==len)
        {
            vis[i]=1;
            if(dfs(0,0,cut+1)) //记住前面可能有没有用到的棍子,所以从0开始(下一个dfs会寻找没有用到的棍子)
                return true;

            vis[i]=0;//我不是很清楚这个地方为什么变回来⊙﹏⊙b汗,如果5是所需的,不用5用3+2会不一样????
                     //还有不变回来会错,求路过大神指点迷津
            return false;  //这是因为上面5是所需,取5没用(接下来拼凑不成),那么取2+3也没有,因为2+3比5灵活
        }
        else
            if(vis[i]==0&&temp+a[i]<len)
        {
            vis[i]=1;
            if(dfs(i+1,temp+a[i],cut))
                return true;

            vis[i]=0;
            if(temp==0)  return false;

            while(a[i]==a[i+1])//如果上一次取5没有,这次取5也拼不好
                i++;
        }
    }
    return false;
}

int main()
{
    int i;
    while(scanf("%d",&n),n)
    {
        sum=0;

        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }

        sort(a,a+n,cmp);  //降序

        for(len=a[0];len<=sum;len++)  //棍子不比比最长的段短
        {
            if(sum%len)  continue;  //肯定是该长度的倍数

            memset(vis,0,sizeof(vis));

            if(dfs(0,0,0))
            {
                printf("%d\n",len);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-08-27 22:34:26

POJ 1011 Sticks(经典dfs)的相关文章

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

POJ 1011 Sticks 【DFS 剪枝】

题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 154895   Accepted: 37034 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now

POJ - 1011 - Sticks (DFS + 剪枝)

题目传送:Sticks 思路:DFS + 剪枝 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include

poj 1011 sticks 经典剪枝问题

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

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

[POJ 1011]Sticks(DFS剪枝)

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 original

poj(1011)——Sticks(经典的dfs+剪枝)

题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. 下面部分是来自于pku的gw老师说哒 1)不要在同一个位置多次尝试同样长度的木棒(在某一次拼接时选择长度为s的木棒导致拼接失败.则在同一位置尝试下一根木棒时.要跳过全部长度为s的木棒) 2)假设因为以后的拼接失败.须要又一次调整第i根棍子的拼法,则不会考虑替换第i根棍子中的第一根木棒. 3)不要希望

POJ 1011 Sticks dfs,剪枝 难度:2

http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先ans需要满足两个条件1.可以被总集合的和sum整除 2.是总集合的某个子集的和 对于条件1,可以通过试除遍历 对于条件2,可以通过dp预筛选,这两个花费时间都不大 接着搜索能不能划分成集合之和恰为ans的若干集合, 1. 可以从大向小找,因为大的更不灵活,而且所有的元素都需要取到 2.比如对于5,

POJ 1011 Sticks(搜索 &amp;&amp; 剪枝 &amp;&amp; 经典)

题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数,那么我们只要去枚举因数然后搜索是否可行即可!具体实现看代码可能更容易看懂,这里不赘述.重要的是体会此类深搜的代码构建过程以及剪枝的考虑的巧妙性! #include<stdio.h> #include<algorithm> #include<string.h> using n