POJ 1011 (DFS + prune)

POJ 1011

Title mean to you n number, not greater than 50. Let you put into m number n groups, each group of the same length len, such that the shortest len

Light time out to write with dfs, pruning shears to many aspects

1: Optimized search order to stick descending order, priority to try long pole

2: of excluding equivalents of the redundant

  1. When a search fails, the next number is equal to the number if a failure, then surely fail

  2. If you fail the first attempt to spell stick recursive branch in the current stick, then direct current branch judgment fails, backtracking immediately

  3. Restrictions has joined a

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int cnt,sum,maxx,len,n;
int vis[500],a[500];

int dfs(int k,int cab,int last)// last排除等效冗余3
{
    if(k > cnt)
    {
        return true;
    }
    if(cab==len)
    {
        return dfs(k+1, 0 ,1);
    }
    int fail=0;   //         排除等效冗余1
    for(int i=last;i<=n;++i)   // last排除等效冗余3
    {
        if(vis[i]==0 && cab+a[i]<=len &&fail!=a[i])
        {
            vis[i]=1;
            if(dfs(k,cab+a[i],i+1))
                return to true ; 
            Fail = A [I]; 
            VIS [I] = 0 ;
             IF (cab == 0 || cab + A [I] == len) // if the cab is 0, or the sum exactly len, but failed , then it must be failed. @ excluding equivalents redundant 2 
                return  to false ; 
        } 
    } 
    return  to false ; 
} 
int main () 
{ 
    iOS :: sync_with_stdio ( to false );
 //     the freopen ( "D: \\ \\ common_text \\ in.txt code_stream "," R & lt ", stdin);
 //     The freopen (" D: \\ \\ code_stream common_text out.txt \\ "," W ", stdout); 
    the while(cin>>n && n)
    {
        int x=0,hhh=0;
        memset(vis,0,sizeof(vis));
        sum=0;
        maxx=0;
        cnt=0,len;

        FOR(i,1,n)
        {
            cin>>x;
            if(x<=50)
            {
                hhh++;
            }
            a[hhh]=x;
            sum+=x;
            maxx=max(maxx,x);
        }
        n=hhh;



        sort(a+1,a+1+n,cmp);
        for(int i=maxx;i<=sum;i++)  //changdu
        {
            if(sum%i==0)
            {
                //cout<<i<<" "<<cnt<<" "<<endl;
                len=i;
                cnt=sum/i;
                 memset(vis,0,sizeof(vis));
                if(dfs(1,0,1)==1)
                    break;
            }
        }

        cout<<len<<endl;
    }


}

 

Root length of the original stick is decreasing (added after the first addition of x and y is added after the first addition of y x are equivalent, only one needs to search)

Guess you like

Origin www.cnblogs.com/jrfr/p/11272129.html