Poj 1011-sticks simple search DFS

Source: Internet
Author: User

Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1011

 

Solution report:

 

After two days of thinking, I read other problem-solving reports and finally figured it out a little bit.

 

Specific ideas, I also do not know, there is a problem-solving report is quite clear, is: http://blog.csdn.net/woshixingaaa/archive/2010/05/14/5589100.aspx

 

 

Below is myCode:

 

# Include <iostream> <br/> # include <algorithm> <br/> # include <fstream> <br/> using namespace STD; </P> <p> int f [65]; <br/> int color [65]; <br/> int CNT; <br/> int totallength; </P> <p> bool CMP (int A, int B) <br/>{< br/> return A> B; <br/>}</P> <p> bool DFS (INT length, int res, int num) // whether the length of F can be composed from res, there are still num wooden sticks <br/>{< br/> If (num = 1) // The remaining wooden sticks need to be solved, because the previous combination is successful, therefore, the remaining combinations are successfully <br/> return true; <br/> If (length = 0) // Successful composition of a Original wooden stick, the remaining number of wooden sticks to be combined is num-1, and then starting from 0, continue to check whether the length can be totallentgh <br/> return DFS (totallength, 0, num-1); <br/> int pre =-1; <br/> int I; <br/> for (I = res; I <CNT; I ++) <br/>{< br/> If (color [I] = 0 & F [I]! = Pre & F [I] <= length) // If the wood stick that uses f [I] to make up the length fails, then f [J], if f [J] = f [I] does not succeed, skip <br/>{< br/> pre = f [I]; <br/> color [I] = 1; <br/> If (DFS (length-f [I], I + 1, num) // success, then exit <br/> break; <br/> color [I] = 0; // failed, color [I] is still restored to the original state <br/> If (RES = 0) // If res = 0, a new wooden stick is combined, if the request fails, it indicates that the length cannot be successful and an error is returned. <br/> return false; <br/>}< br/> if (I = CNT) // I enumeration to CNT, failed <br/> return false; <br/> else <br/> return true; <br/>}</P> <P> int main () <br/>{< br/> while (CIN> CNT) & CNT! = 0) <br/>{< br/> int sum = 0; // The total length of the stick <br/> for (INT I = 0; I <CNT; I ++) <br/>{< br/> CIN> F [I]; <br/> sum + = f [I]; <br/>}< br/> sort (F, F + CNT, CMP ); // arrange from large to small <br/> for (INT I = f [0]; I <= sum; I ++) // The length of the stick should be as small as possible, but it is obviously greater than or equal to the maximum value in F [] <br/>{< br/> If (sum = I) <br/>{< br/> cout <sum <Endl; <br/> break; <br/>}< br/> If (sum % I = 0) <br/>{< br/> totallength = I; <br/> for (Int J = 0; j <CNT; j ++) <br/> color [J] = 0; // access status of the captured wooden stick <br/> If (DFS (totallength, 0, sum/I )) // from F [0], the sum/I Original wooden stick with totallength length is successfully formed <br/>{< br/> cout <totallength <Endl; <br/> break; <br/>}</P> <p >}< br/>}

 

 

Appendix:

Sticks
Time limit:1000 ms   Memory limit:10000 K
Total submissions:72642   Accepted:15966

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 shoshould contains the smallest possible length of original sticks, one per line.

Sample Input

 
95 2 1 5 2 1 5 2 141 2 3 40

Sample output

 
65

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.