hdu 1518 Square

Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17297    Accepted Submission(s): 5419


Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
 

 

Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
 

 

Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
 

 

Sample Input
3 4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5
 

 

Sample Output
yes no yes
 
//剪枝很重要
#include <iostream>
#include <algorithm>
using namespace std;
long long a[25],n,s;
bool v[25];
int m;
bool cmp(long long x,long long y)
{
    return x>y;
}
int dfs(long long h, int w, int k)  
{  
    if(h == s)  
    {  
        w = 1;  
        h = 0;
        k++;  
        if(k == 4)  
            return 1;  
    }  
  
    for(int i=w; i<=m; i++)  
    {  
        if(!v[i])  
        {  
           v[i] = 1;  
            if((h+a[i] <= s ) && dfs(h+a[i], i+1, k))  //有一根无法和其他的拼就直接跳出来
                //而不是一根根选
                return 1;  
           v[i] = 0;  
        }  
    }  
    return 0;  
}  
int main()
{
    scanf("%lld",&n);
    while(n--)
    {
        scanf("%d",&m);
        s=0;
        int i;
        for(i=1;i<=m;i++)
        {
            scanf("%lld",&a[i]);
            v[i]=0;
            s=s+a[i];
        }
        if(s%4!=0) {printf("no\n");continue;}
        s=s/4;
        for(i=1;i<=m;i++)
        {
            if(a[i]>s) {printf("no\n");break;}
        }
        if(i!=m+1) continue;
        sort(a+1,a+m+1,cmp);
        if(a[0]+a[m-1]>s) {printf("no\n");break;}    
        if(dfs(0,1,0)) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

 

posted on 2018-02-03 22:56  蔡军帅  阅读(96)  评论(0编辑  收藏  举报