[回溯剪枝] pku1011 解题报告

Sticks
Time Limit:1000MS  Memory Limit:10000K
Total Submit:24457 Accepted:5494

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

对于准备学习回溯剪枝的人来说,这道题是再适合不过了 :)
s[]记录数据,将数据按递减顺序排序,搜索数d,len = sum/d
目标:搜索len的组合

解题思路:
 1. res+s[i] == d
 搜索成功返回1,否则返回0
 2. res+s[i] < d
 选取s[i]结点, 继续从下一点开始搜索,成功则返回1;
 如果失败,判断本次搜索起始是否是一个搜索起始段,即res=0,如果是,则
返回0。因为此时s[i]必为从最左端开始搜索,第一个还没有被用的点,故而如果
有解,则必然包含s[i],所以,本次搜索如果失败,在res=0的情况下,返回0
减少不必要的搜索。
#include <cstdio>
#include 
<string>
#include 
<algorithm>
using namespace std;

int s[65];
int b[65];
int n, d, len;

inline 
int search(int k, int res, int cnt)
{
    
if(k>=n)
        
return 0;
    
if(cnt == len)
        
return 1;

    
int f = 0;
    
if(res == 0)
        f 
= 1

    
for(int i=k; i<n; ++i)
        
if(!b[i])
        
{                
            
if(s[i]+res == d)
            
{
                b[i] 
= 1;
                
if(search(00, cnt+1))
                    
return 1;    
                b[i] 
= 0;    
                
return 0;
            }
                
            
else if(res+s[i] < d)
            
{
                b[i] 
= 1;
// 选择s[i]结点,继续搜索len组合
                
if(search(i+1, res+s[i], cnt))
                    
return 1;
                b[i] 
= 0;
// 在res=0,即段搜索起点的情况下,如果搜索失败则返回0。
// 因为此时,s[i]是从0开始搜索第一个还没有被用的结点,
// 如果有解,本次搜索必然包含s[i],否则必然无解。
                
if(f)
                    
return 0;
                
while(i<n-1 && s[i]==s[i+1])  
                    
++i;
            }

        }

    
return 0;
}


inline 
bool cmp(int a, int b)
{
    
if(a>b)
        
return true;
    
else
        
return false;
}


int main()
{
    
while(1)
    
{
        scanf(
"%d"&n);
        
        
if(!n)
            
break;        

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


        sort(
&s[0], &s[0]+n, cmp);
        
        
for(int i=s[0]; i<=sum; ++i)        
            
if(sum%== 0)
            
{    
                d 
= i;
                len 
= sum/i;
                
                memset(b, 
0sizeof(b));
                
if(search(001))
                
{                    
                    printf(
"%d", i);
                    
break;
                }

                
            }

    }

    
    
return 0;
}



 

你可能感兴趣的:(algorithm,c/c++,search,output,input,algorithm,string,file)