TZOJ 1822 Sticks DFS plus pruning

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.

Entry

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.

Export

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

Meaning of the questions: The following components are the same length (minimum)

#include <the iostream> 
#include <algorithm> 
#include <cstdio> 
#include <CString>
 the using  namespace STD;
 const  int Max = 65 ; 
 
int n-, len, Stick [Max];
 BOOL In Flag, VIS [Max]; 
 
BOOL CMP ( int a, int B) 
{ 
    return a> B; 
} 
 
void DFS ( int DEP, int now_len, int U) 
{    // DEP small number of rods currently being used, u is currently to be processed rods. 
    IF (Flag) return; // complete recursive return condition 
    IF (now_len == 0 ) {                     //   current length of 0, the next looking up a small current bar. 
        int K = 0 ;
         the while (VIS [K]) K ++;               //   find the first maximum current small stick. 
        VIS [K] = to true ; 
        DFS (DEP + . 1 , Stick [K], K + . 1 ); 
        VIS [K] = to false ;
         return ; 
    } 
    IF (now_len == len) 
    {                   //   this length len, i.e., and It has become a patchwork of original stick. 
        if(DEP == n) = In Flag to true ;         //   complete flag: all n has a small stick to fight. 
        the else DFS (DEP, 0 , 0 );
         return ; // unfinished recursive return conditions 
    }
     for ( int I = U; I <n-; I ++ )
         IF (VIS [I] && now_len Stick + [I]! <= len) // filter conditions 
        {
             IF (VIS [I-! . 1 ] && Stick [I] == Stick [I- . 1 ]) Continue ;       //   Unique Search: the most important pruning. 
            VIS [I] = to true ; 
            DFS (DEP+ 1, now_len + stick[i], i + 1);
            vis[i] = false;
        }
}
 
int main()
{
    while(scanf("%d", &n) && n != 0)
    {
        int sum = 0;
        flag = false;
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &stick[i]);
            sum = + Stick [I]; 
        } 
        Sort (Stick, Stick + n-, CMP);      //   descending order. 
        for (len = Stick [ 0 ]; len <sum; len ++ )
             IF (% sum len == 0 ) //    here is equivalent to prune (filter condition) 
            {           //   Enumeration sum can be divided by the length. 
                Memset (VIS, 0 , the sizeof (VIS)); 
                DFS ( 0 , 0 , 0 );
                 IF (In Flag) BREAK ; 
            } 
        the printf ("%d\n", len);
    }
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/lhlccc/p/11567342.html