HJDU 1518—— Square(DFS)

简单深搜~~~~

<strong>#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<functional>
#define M 30
using namespace std;
int vis[M],a[M],n,flag,sum;
void dfs(int s,int cur,int d)
{
    if(cur==3||flag){
        flag=1;
        return ;
    }
    for(int i=s;i<n;++i){

        if(vis[i]||(d+a[i]>sum)) continue ;
        vis[i]=1;
        if(d+a[i]==sum) dfs(0,cur+1,0);
        else dfs(i+1,cur,d+a[i]);
        if(flag) return ;
        vis[i]=0;
        if(d==0) break; //超强力剪枝  瞬间时间变为46MS
        while(a[i]==a[i+1]) i++; //剪枝

    }
    return ;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--){
        cin>>n;
        sum=0;
        for(int i=0;i<n;++i){
            scanf("%d",&a[i]);
            sum+=a[i];
            vis[i]=0;
        }
        if(sum%4){
            printf("no\n");
            continue;
        }
        sum/=4;
        sort(a,a+n,greater<int>());
        if(a[n-1]>sum){
            printf("no\n");
            continue;
        }
        flag=0;
        dfs(0,0,0);
        if(flag) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}</strong>

HJDU 1518—— Square(DFS)

时间: 2024-10-25 05:10:42

HJDU 1518—— Square(DFS)的相关文章

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

HDU 1518 Square (DFS+剪枝)

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 intege

hdu4393Digital Square(dfs)

Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1827 Accepted Submission(s): 714 Problem Description Given an integer N,you should come up with the minimum nonnegative integer M.M m

HDOJ 题目4394 Digital Square(DFS)

Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1757    Accepted Submission(s): 677 Problem Description Given an integer N,you should come up with the minimum nonnegative integer

HDU1518 Square(DFS)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11151    Accepted Submission(s): 3588 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-en

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

杭电 1518 Square (深搜)(回溯)

http://acm.hdu.edu.cn/showproblem.php?pid=1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8343    Accepted Submission(s): 2706 Problem Description Given a set of sticks of various

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar