Poj 1011 sticks classic DFS + pruning

Source: Internet
Author: User

When I first met this question, I had no idea at all. I did it only after reading the report on solving the problem .. No idea .. You can only learn from others ..

After reading this article, we should be able to thoroughly understand this question .. : Http://apps.hi.baidu.com/share/detail/17119728

Below is myCode:

# Include <iostream>
# Include <stdlib. h>
# Include <string. h>
Using namespace STD;

# Define n 70
Int sticks [N]; // used to store the length of a wooden stick
Int used [N]; // used to record whether the wood stick has been used
Int N, CNT, M, Len, flag;

Int CMP (const void * a, const void * B)
{
Return * (int *) B-* (int *);
}

Void DFS (INT POs, int current, int CNT) // POS is used to record the position in the current sticks [] array. Current is used to record the length of the currently matched wooden stick, CNT record to match the number of sticks
{

If (CNT = m)
Flag = true; // if the current length matches exactly all the wood sticks, the returned result is the completion task. The current length is the minimum length to be matched.
Else if (current = Len) // if current is exactly the same as Len, a match is completed, CNT + 1;
DFS (0, 0, CNT + 1 );
Else
{
Int pre =-1; // record the value previously searched. The value already searched in this matching process does not need to be matched if the length is the same.
For (INT I = Pos; I <n; I ++)
{
If (! Used [I] & sticks [I]! = Pre & Current + sticks [I] <= Len) // very important pruning conditions, which cannot be ignored. pruning techniques and art
{
Used [I] = 1;
Pre = sticks [I];
DFS (Pos + 1, current + sticks [I], CNT );
Used [I] = 0; // The search is unsuccessful and the use flag is restored.
If (Pos = 0 | flag) // returns if only one wooden stick is found or the search is successful.
Return;
}
}
}
}

Int main ()
{
While (CIN> N & n> 0)
{
Int sum = 0;
For (INT I = 0; I <n; I ++)
{
Cin> sticks [I];
Sum + = sticks [I];
}
Memset (used, 0, sizeof (used ));
Qsort (sticks, N, sizeof (sticks [0]), CMP); // The length of the wooden rod is sorted by the length from large to small, according to the meaning of the question, the minimum length of the original wooden rod is at least the length of the longest wooden rod after being truncated, and the maximum length is the length and
Flag = false; // indicates whether the operation is successful.
For (LEN = sticks [0]; Len <= sum; Len ++) // searches one by one from the possible length of the original wood stick. Once the loop is successfully stopped, the minimum result is obtained.
{
If (sum % Len = 0) // an important pruning condition that cannot be ignored can greatly improve the efficiency.
{
M = sum/Len;
DFS (0, 0 );
If (FLAG)
Break;
}
}
Cout <Len <Endl;
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.