zoukankan      html  css  js  c++  java
  • 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
    ");continue;}
            s=s/4;
            for(i=1;i<=m;i++)
            {
                if(a[i]>s) {printf("no
    ");break;}
            }
            if(i!=m+1) continue;
            sort(a+1,a+m+1,cmp);
            if(a[0]+a[m-1]>s) {printf("no
    ");break;}    
            if(dfs(0,1,0)) printf("yes
    ");
            else printf("no
    ");
        }
        return 0;
    }
  • 相关阅读:
    获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
    获取所有员工当前的manager,如果当前的manager是自己的话结果不显示
    String笔记02
    String笔记
    ArrayList笔记
    猜数字小游戏
    标准类
    封装
    面向对象
    方法
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8411115.html
Copyright © 2011-2022 走看看