POJ1014 Problem Solving report (DFS)

Source: Internet
Author: User

Title here: http://poj.org/problem?id=1014

To see test instructions, the title of the input is the value of the number of 1,2,3,4,5,6 marble, instead of 6 value for the input number of marble! Choose this topic mainly want to practice a deep search. On the internet to see a piece of code, deep search using the greedy algorithm although it can be AC but there are a lot of loopholes:

For example, test data (0 0 3 0 3 1) An error occurs:

Search time will be selected: The value of each of the 6,5,3 to determine that can not be divided. But the right way to split it should be 6+3+3+3=15.

This is a vulnerable code on the Internet:

1#include <iostream>2 using namespacestd; 3   4 intamount[7] = {0}; 5 intHalf_value =0; 6 intFlag =0; 7   8 voidDFS (intValueintpre) {  9   Ten     if(Value = =half_value) {   OneFlag =1;  A         return;  -     }   -    the     if(Flag = =1){//This can be removed from the AH!  -         return;  -     }   -    +     inti =0;  -      for(i = pre; I >0; i--){   +         if(Amount[i]) { A             if(i + Value <=half_value) {   atamount[i]--;  -DFS (i +value, i);  -    -                 if(Flag = =1){//no less, feel its role, let the recursive stack end all DFS -                     return;  -                 }   in             }   -         }   to     }   + }   -    the    * intMain () { $   Panax Notoginseng     intTestCase =1;  -      while(true){   theFlag =0;  +         intTotalvalue =0;  A         intN =6;  the         inti =1;  +          while(I <=N) {   -CIN >>Amount[i];  $Totalvalue + = amount[i] *i;  $i++;  -         }   -    the         if(!amount[1] &&!amount[2] &&!amount[3] &&!amount[4] &&!amount[5] &&!amount[6]){   -              Break; Wuyi         }   the    -printf"Collection #%d:\n", testcase++);  Wu         if(Totalvalue%2!=0){   -cout <<"Can ' t be divided."<< Endl <<Endl;  About             Continue;  $         }   -    -Half_value = Totalvalue/2;  -Daso0,6);  A    +         if(flag) { thecout <<"Can be divided."<<Endl;  -}Else {   $cout <<"Can ' t be divided."<<Endl;  the         }   thecout <<Endl;  the     }      the     return 0;  -}
a vulnerable Code

In order to solve this problem, before the decision can not be divided into a cycle, that is, from the value of 5,4...1 marble began deep search, so as to avoid the above code loopholes. Because the greedy algorithm basically solves most of the situation, the loop does not cause a timeout. The following is the modified code:

#include <iostream>#include<string.h>#include<stdio.h>using namespacestd;intamount[7] = {0 };inttemp_amount[7] = {0};//Save the original input valueintHalf_value =0;intFlag =0;voidDasointValueintpre) {    if(Value = =half_value) {Flag=1; return; }    inti =0;  for(i = pre; I >0; i--)    {        if(Amount[i]) {if(i + Value <=half_value) {Amount[i]--; DFS (i+value, i); if(Flag = =1)                {                      return; }            }        }    }}intMain () {intTestCase =1;  while(true) {flag=0; intTotalvalue =0; intN =6; inti =1;  while(I <=N) {cin>>Temp_amount[i]; Totalvalue+ = temp_amount[i] *i; I++; }        if(!temp_amount[1] &&!temp_amount[2] &&!temp_amount[3] &&!temp_amount[4] &&!temp_amount[5] &&!temp_amount[6])        {             Break; } printf ("Collection #%d:\n", testcase++); if(Totalvalue%2!=0) {cout<<"Can ' t be divided."<< Endl <<Endl; Continue; } half_value= Totalvalue/2; memcpy (Amount, Temp_amount,7*sizeof(int)); DFS (0,6); if(flag) cout<<"Can be divided."<<Endl; Else        {             for(intm =6; M >=1; m--)            {                if(!flag) {memcpy (amount, Temp_amount,7*sizeof(int));//because the amount array has already been manipulated, it needs to be restoredDaso0, M); }            }            if(!flag) cout<<"Can ' t be divided."<<Endl; Elsecout<<"Can be divided."<<Endl; } cout<<Endl; }    return 0;}

(To be supplemented later, uh)

POJ1014 Problem Solving report (DFS)

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.