POJ 1011/2362 dfs+ Pruning (stick)

Source: Internet
Author: User

Test instructions: George brought a set of equal-length sticks and cut them randomly, making each stick less than 50 units in length. He then tried to restore the sticks to a pre-cut state, but forgot how many sticks and the initial lengths of the sticks were initially. Please design a program to help George calculate the possible minimum length of the stick. The length of each stick is represented by an integer greater than 0. (2362 is a special case of 1011, ask if a pile of sticks can be square)

Idea: dfs+ pruning. Where pruning has considerable finesse, one of the places where the pruning did not think of led to tle multiple times.

Several obvious pruning points (set ans as the final answer):

1, ans>= The maximum length of this pile of sticks

2, ans<= the sum of the length of the pile of sticks

3. Ans must be divisible by the sum of the lengths of the sticks.

4, the stick from the long to the short sort, first consider the long can effectively prune

Two less obvious but most important pruning points are indispensable:

5. If a stick of length x has just been added and then the search fails, then the next search is only a small stick (the function of the pre variable in the code).

6, in each building a new length for the ans of the stick, check the new stick of the first s[i], if after searching all the sticks can not be combined, then the s[i] can not be combined in the current combination, not down search (because how to search s[i] will not be added to any of the sticks). The code in the IF ( !from) return 0, the function of this sentence.

1011:

#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX (b) ((a) > (c)? ( A):(B)) #define N 65int s[n],f[3000],n,used[3005],res[6][100];int sum,len,stick,perlen;int cmp (const void* a,const void * b) {return (* (int*) b)-(* (int*) a);}    void factor (int b,int n) {int i; for (i = b,len=0;i<=n;i++) if (n%i==0) f[++len] = i;}    int dfs (int now,int from,int num) {///The length of the current stick has been spelled/the number of sticks started from the first stick/The amount of the stick has been spelled int i,pre;    if (num = = stick) return 1;    if (now = = Perlen) return Dfs (0,0,NUM+1);  for (i = from,pre=0;i<n;i++) if (!used[i] && s[i]!=pre && now+s[i]<=perlen) {Used[i]            = 1;            if (Dfs (Now+s[i],i+1,num)) return 1;            Used[i] = 0;             Pre = S[i];        Pruning 5, one of the most important pruning if (!from)//Pruning 6, the most important pruning of the two return 0; } return 0;}        int main () {while (scanf ("%d", &n) &&n) {int i;        sum = 0; FoR (i = 0;i<n;i++) {scanf ("%d", &s[i]);        Sum + = S[i]; } qsort (s,n,sizeof (int), CMP);           Satisfying pruning 4 factor (s[0],sum);            The sieve factor, which satisfies the pruning for (i = 1;i<len;i++) {memset (used,0,sizeof (used));       Stick = Sum/f[i];          Current search under the number of sticks Perlen = f[i];        The current search under each stick length if (Dfs (0,0,0)) break;    } printf ("%d\n", F[i]); } return 0;}


2362:

#include <stdio.h> #include <string.h> #include <stdlib.h>int s[25],used[25];int t,n,sum;int CMP ( Const void* A,const void* b) {return (* (int*) b)-(* (int*) a);    int dfs (int now,int from,int num) {int i,pre;    if (num = = 4) return 1;    if (now = = SUM/4) return Dfs (0,0,NUM+1); for (i = from,pre=0;i<n;i++) if (!used[i] && s[i]!=pre && now+s[i]<=sum/4) {Used[i]            = 1;            Pre = S[i];            if (Dfs (Now+s[i],i+1,num)) return 1;            Used[i] = 0;        if (!from) return 0; } return 0;}    int main () {scanf ("%d", &t);        while (t--) {int i;        memset (used,0,sizeof (used));        scanf ("%d", &n);            for (i = sum = 0;i<n;i++) {scanf ("%d", &s[i]);        Sum + = S[i];        } qsort (s,n,sizeof (int), CMP);        if (sum%4==0 && dfs (0,0,0)) printf ("yes\n");    else printf ("no\n"); } RETUrn 0;} 



POJ 1011/2362 dfs+ Pruning (stick)

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.