HDU1518 Square 【剪枝】

Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8900    Accepted Submission(s): 2893

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

Source

University of Waterloo Local Contest 2002.09.21

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

#define maxn 22

int L[maxn], n, tar, times;
bool vis[maxn], ok;

bool DFS(int k, int leftLen) {
    if(!leftLen) {
        if(++times == 4) return true;
        for(int i = 1; i < n; ++i) {
            if(!vis[i]) {
                vis[i] = 1;
                if(DFS(i + 1, tar - L[i]))
                    return true;
                else {
                    --times;
                    vis[i] = 0;
                    return false;
                }
            }
        }
    }

    int i;
    for(i = k; i < n; ++i) {
        if(!vis[i] && L[i] <= leftLen) {
            vis[i] = 1;
            if(L[i-1] == L[i] && !vis[i-1]) {
                vis[i] = 0;
                continue;
            }
            if(DFS(i+1, leftLen - L[i]))
                return true;
            vis[i] = 0;
        }
    }

    return false;
}

int main() {
    int t, i;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        tar = 0;
        for(i = 0; i < n; ++i) {
            scanf("%d", &L[i]);
            vis[i] = 0; tar += L[i];
        }

        if(tar % 4) {
            printf("no\n");
            continue;
        }
        tar /= 4;

        std::sort(L, L + n, std::greater<int>());
        if(L[0] > tar) {
            printf("no\n");
            continue;
        }

        times = 0; vis[0] = 1;
        DFS(1, tar - L[0]);

        printf(times == 4 ? "yes\n" : "no\n");
    }
    return 0;
}
时间: 2024-12-31 07:21:44

HDU1518 Square 【剪枝】的相关文章

hdu1518 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): 7839    Accepted Submission(s): 2526 Problem Description Given a set of sticks

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

HDU1518:Square(DFS)

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 88   Accepted Submission(s) : 37 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vari

hdu1518(Square)深搜+剪枝

点击打开杭电1518 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,

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]

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

hdu 1518 Square 深搜,,,,花样剪枝啊!!!

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

Project Euler 92:Square digit chains C++

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

解数独算法的实现——剪枝优化

最近人工智能做个小实验,组队选了个数独游戏,顺便研究了一下.解数独感觉主流思想也就是深搜回溯了吧,优化就是各种剪枝方法. 1 引言 数独起源于18世纪初瑞士数学家欧拉等人研究的拉丁方阵(Latin Square),曾风靡日本和英国.现有解法包括基础解法:摒除法,余数法,进阶解法:区块摒除法(Locked Candidates).数组法(Subset).四角对角线(X-Wing).唯一矩形(Unique Rectangle).全双值坟墓(Bivalue Universal Grave).单数链(X