UVA-307 sticks (dfs+ pruning)

Source: Internet
Author: User

The main topic: with n length may not be equal to match the maximum number of sticks.

Title Analysis: Enumerate the lengths of all possible equal-length sticks, match each root by DFS, and prune them in the process. The length of the stick is sorted from large to small, that is to say, it is always preferred to match each long stick. The pruning scheme is as follows: 1. If the i-1 stick is not used in the matching of the current scheme and length[i]==length[i-1], then the root of the first stick is also impossible to use; 2. If the successful CNT root is matched to a long stick, and the first cnt+1 root match is unsuccessful, then pruning; 3. If the match is in the cnt+ 1 sticks, the longest stick stick will be the first to be selected, if at this time cnt+1 root and other long stick match failure, then the first cnt+1 root and other long sticks do not need to continue matching, return to the first CNT root and other long stick matching can, because the match with stick in this matching the range of the optional stick is larger than the future , this is not matched, later more matching is not, to re-match the first CNT stick, to produce a new longest stick stick ', and then try to match the cnt+1 root and other long sticks.

The above three is indispensable!!!

The code is as follows:

# include<iostream># include<cstdio># include<vector># include<cstring># include< Algorithm>using namespace Std;int w[100],n,vis[100];bool mycomp (const int &a,const int &b) {return a>b;}    BOOL Dfs (int cur,int g,int id,int cnt,int sum) {if (g*cnt==sum) return true;        for (int i=id;i<n;++i) {if (vis[i]) continue;        if (I&&!vis[i-1]&&w[i-1]==w[i])///pruning 1 continue;            if (cur+w[i]==g) {vis[i]=1;            if (Dfs (0,g,0,cnt+1,sum)) return true;            vis[i]=0;            return false;///Pruning 2} else if (cur+w[i]<g) {vis[i]=1;            if (Dfs (cur+w[i],g,i+1,cnt,sum)) return true;            vis[i]=0; if (cur==0) return false;///pruning 3}} return false;    int main () {int sum;        while (~SCANF ("%d", &n) &&n) {sum=0; for (int i=0;i<n;++i) {scanf ("%d ", w+i);        Sum+=w[i];        } sort (W,w+n,mycomp);        int flag=0;                for (int g=w[0];g<=sum/2;++g) {if (sum%g==0) {memset (vis,0,sizeof (VIS));                    if (Dfs (0,g,0,1,sum)) {flag=1;                    printf ("%d\n", g);                Break    }}} if (!flag) printf ("%d\n", sum); } return 0;}

  

UVA-307 sticks (dfs+ pruning)

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.