I don't know how to use DP, but DFS is very useful. The DFS idea is obvious, search, record, if just find half of the total value of the search to indicate success.
The main topic: 6 numbers per group, representing the number of items worth 1 to 6 respectively. Now ask if you can divide it by value.
Sample Input//6 The number of value items, all at 0 o'clock end
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output//Note format, empty two lines
Collection #1: Can ' t be divided.
Collection #2: Can be divided.
1#include <iostream>2 using namespacestd;3 4 intn[7];//Storage Quantity5 intsum,t=1, I;6 BOOLFlag//Mark7 8 voidDfsintSintp)9 {Ten if(s==sum/2)//found the value of just half One { Aflag=true; - return; - } the for(inti=p;i>=1; i--)//search, looks like a DP. - { - if(N[i]) - { + if(s+i<=sum/2) - { +n[i]--; ADFS (s+i,i); at if(flag) - Break; - } - } - } - return; in } - to + intMain () - { the while(1) * { $sum=0;Panax Notoginseng for(i=1; i<=6; i++) - { theCin>>N[i]; +Sum+=i*n[i];//figure out the sum of values A } the if(! Sum)//all 0 + Break; - if(sum%2)//The total value is odd when not established, pruning $ { $cout<<"Collection #"<<T<<": \ncan ' t be divided.\n\n"; -t++; - Continue; the } - Wuyiflag=false; the -Dfs0,6); Wu - if(flag) About { $cout<<"Collection #"<<T<<": \ncan be divided.\n\n"; -t++; - Continue; - } A Else + { thecout<<"Collection #"<<T<<": \ncan ' t be divided.\n\n"; -t++; $ Continue; the } the } the return 0; the}
View Code
2016HUAS_ACM Summer Camp 4K-Basic DP