1. 程式人生 > >poj-1011 sticks(搜索題)

poj-1011 sticks(搜索題)

留下 NPU locks 從大到小 一個接一個 失敗 original 我們 一個

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

題意
給出一定數量的小木棒的長度,它是由等長的若幹木棒隨意砍斷所得到的。對於給定的一組小木棒,請求出原始木棒的最小長度。
題解
dfs搜索,
思想很簡單,一個接一個的把木棍拼起來,最後把木棍用光。關鍵的地方是幾個剪枝技巧:設所有木棍的總長度為 Sum, 最終的答案(長度)是 L。 
1. 首先要明白, Sum一定要能被 L 整除。 
2. L 一定 大於等於 題目給出的最長的木棍的長度 Max。由上述兩點,我們想到,可以從 Max 開始遞增地枚舉 L, 直到成功地拼出 Sum/L 支長度為 L 的木棍。
搜索中的剪枝技巧: 3. 將輸入的輸入從大到小排序,這麽做是因為一支長度為 K 的完整木棍,總比幾支短的小木棍拼成的要好。形象一些:如果我要拼 2 支長為8的木棍,第一支木棍我拼成 5 + 3 然後拼第二支木棍但是失敗了,而我手中還有長為 2 和 1 的木棍,我可以用 5 + 2 + 1 拼好第一支,再嘗試拼第二支,仔細想一想,就會發現這樣做沒意義,註定要失敗的。我們應該留下 2+1 因為 2+1 比 3 更靈活。 4. 相同長度的木棍不要搜索多次, 比如:我手中有一些木棍, 其中有 2 根長為 4 的木棍, 當前搜索狀態是 5+4+.... (即表示長度為 5,4,2 的三支拼在一起, ...表示深層的即將搜索的部分), 進行深搜後不成功,故我沒必要用另一個 4 在進行 5+4+...5. 將開始搜索一支長為 L 的木棍時,我們總是以當前最長的未被使用的 木棍開始,如果搜索不成功,那麽以比它短的開始那麽也一定不能取得全局的成功。因為每一支題目給出的木棍都要被用到。如果,有4 5 4 4 3 2想拼成長為 6 的木棍,那麽從 5 開始, 但是顯然沒有能與 5一起拼成 6 的,那麽我就沒必要去嘗試從 4 開始的,因為最終 5 一定會被遺棄。在拼第 2 3 ... 支木棍時,一樣。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define PI 3.14159265358979323846264338327950

int len[66];
bool vis[66];
int sum,l,n;

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

bool dfs(int m,int left)
{
    if(m==0 && left==0)
        return true;
    if(left==0)
        left=l;
    for(int i=0;i<n;++i)
    {
        if(!vis[i] && len[i]<=left)
        {
            if(i>0)
            {
                if(!vis[i-1] && len[i]==len[i-1])
                    continue;
            }
            vis[i]=true;
            if(dfs(m-1,left-len[i]))
                return true;
            else
            {
                vis[i]=false;
                if(left==len[i]||left==l)     //重要剪枝,不佳這句會超時
                    return false;
            }
        }
    }
    return false;
}

int main()
{
    while(scanf("%d",&n) && n)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&len[i]);
            sum=sum+len[i];
        }
        qsort(len,n,sizeof(int),cmp);
        for(l=len[0];l<=sum/2;++l)
        {
            if(sum % l)
                continue;
            memset(vis,false,sizeof(vis));
            if(dfs(n,l))
            {
                printf("%d\n",l);
                break;
            }
                
        }
        if(l>sum/2)
            printf("%d\n",sum);
    }
}

poj-1011 sticks(搜索題)