您的位置:首页 > 其它

ACM: 一题DFS(深搜,剪枝) poj 1011

2016-05-19 23:17 405 查看
 

                                                                             
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

题意: 给你n根小木棒,找出能恰好拼凑成最大的长度.

         
例如第一个例子: (5,1) , (5,1) , (5,1) , (2,2,2) => 6

                  
第二个例子: (2,3) , (1,4) => 5

解题思路:

               
1. 一开始想到是动态规划,像背包问题.

               
2. 用深搜的思想试一试,木棒长度的尝试法.  len = stick[0] ; len
<= sum; len++;

               
3. dfs(深搜就要涉及剪枝问题).

                   
递归的参数: 尝试长度len , 离目标还需要长度reLen , 木棒剩余条数: num;

      
问题分析:

          
(1).递归入口: 因为木棒拼凑成的条数是整数,所以一开始可以剪枝一次,sum % len == 0

          
(2).确定递归的出口是: 当剩余木棒条数为0时, 并且reLen = 0;

       
循环递归中:

          
(3).当木棒被访问过continue;

          
(4).reLen 必须要大于等于 当前木棒的长度(reLen >= stick[i]);

          
(5).在(4)的基础上可以再次剪枝 stick[i] == reLen || reLen == len

          
(

             
解释: 本来表示即将拼成一根棒,或刚进行新一轮拼凑

                      
(即if中判断的是主问题性质完全相同的子问题)

                      
但经过上面的if判断,子问题不能完成所有任务,

                      
那么整体不可能完成任务,不再考虑,搜索失败(剪枝)

            
)

          
(6).将相同长度的木棒跳跃(剪枝);(相同不能解决问题的子问题进行剪枝).

代码:

#include <cstdio>

#include <iostream>

#include <cstdlib>

#include <cstring>

using namespace std;

#define MAX 70

int stick[MAX];

bool visited[MAX];

int  n;

int sum;

int cmp(const void *a,const void *b)

{

    return
*(int*)b - *(int*)a;

}

int dfs(int len,int reLen,int num)

{

    if(reLen ==
0 && num == 0)

       
return len;

    if(reLen ==
0)

       
reLen = len;

       

    for(int i =
0; i < n; ++i)

    {

       
if(visited[i] == 1)

           
continue;

       
if(reLen >= stick[i])

       
{       

           
visited[i] = 1;

           
if(dfs(len,reLen-stick[i],num-1) != 0)

               
return len;

           
visited[i] = 0;

           
if(stick[i] == reLen || reLen == len)

               
break;

           
while(stick[i] == stick[i+1])

               
i++;

       
}

    }

    return
0;

}

int main()

{

//   
freopen("input.txt","r",stdin);

   
while(scanf("%d",&n) != EOF
&& n != 0)

    {

       
sum = 0;

       
for(int i = 0; i < n; ++i)

       
{

           
scanf("%d",&stick[i]);

           
sum += stick[i];

       
}

       
qsort(stick,n,sizeof(stick[0]),cmp);

       
int result;

       
for(int len = stick[0]; len <= sum; ++len)

       
{

           
if(sum % len == 0)

           
{

               
result = 0;

               
memset(visited,0,sizeof(visited));

               
result = dfs(len,0,n);

               
if(result != 0)

                   
break;

           
}

       
}

       
printf("%d\n",result);

    }

   

    return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: