Blue Bridge Cup-Sticks (Search)

题目:
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.
输入格式:
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.
输出格式:
The output should contains the smallest possible length of original sticks, one per line.

Idea: This question mainly requires calculating the original y wooden sticks of the same length x from n small wooden sticks that have been randomly divided. Because there may be many solutions, he asks you to find the smallest possible value of y. The idea of ​​enumeration is the most basic and easiest to use method, but how to enumerate more efficiently also requires thinking. We should start from the largest of n small wooden sticks and enumerate with the sum of all small wooden sticks as the ending point, that is, between maxi~sum . Then after selecting any x, judge whether it can be divisible by the total length, if not, it means that it cannot be restored, skip directly and select the next one. If yes, it means it may be possible to restore. Use dfs to search in depth to judge. In order to avoid unnecessary time consumption, the search at this time needs to use the pruning method. For example, they are all sorted from largest to smallest. At this time, the selected small stick a[i] is equal to the previous a[i-1], However, the last small stick book[i] is not marked, that is, there is no choice, then it does not need to continue searching, it can be skipped directly.
code show as below:

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int n,x,flag,a[101],book[101];
void dfs(int deep,int len,int dex)
{
    
    
 int i;
 if(len==0)
 {
    
    
  i=1;
  while(book[i])
  i++;
  book[i]=1;
  dfs(deep+1,len+a[i],i+1);
  book[i]=0;
  return;    //及时返回,避免不不必要的搜索
 }
 if(len==x)
 {
    
    
  if(deep==n)
  {
    
    
   flag=1;
  }
  else {
    
    
   dfs(deep,0,1);
  }
  return;
 }
 for(i=dex;i<=n;i++)
 {
    
    
  if(book[i])
  continue;
  if(len+a[i]<=x)
  {
    
    
   if(a[i]==a[i-1]&&book[i-1]==0)
   continue;
   book[i]=1;
   dfs(deep+1,len+a[i],i+1);
   book[i]=0;
   if(flag)
   return;
   if(len+a[i]==x)
   return;
  }
 }
 return;
}
bool cmp(int a,int b)
{
    
    return a>b;}
int main()
{
    
    
 int i,sum;
 while(cin>>n||n)
 {
    
    
  sum=0;
  for(i=1;i<=n;i++)
  {
    
    
   cin>>a[i];
   sum+=a[i];
  }
  sort(a+1,a+n+1,cmp);
  for(i=a[1];i<=sum;i++)
  {
    
    
   if(sum%i)
   continue;
   x=i;
   memset(book,0,sizeof(book));
   flag=0;
   dfs(0,0,1);
   if(flag)
   {
    
    
    cout<<i<<endl;
    break;
   }
  }
 }
 return 0;
}

Input example:
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Output example:
6
5

Guess you like

Origin blog.csdn.net/HT24k/article/details/106991974