POJ 2362:Square(DFS)

问所给木棒能否构成一个正方形

难点在于木棒可以拼接,比如2 2 1 1 2也是能构成正方形的

这题的剪枝有点意思:

第一是提前判断所给的木棒长度之和能否被4整除,不能直接输出NO

第二是只需满足上面的条件后,只需要找到3条边就行了,剩下一条边自然也符合

l表示已找到的长度,cnt为已找到的边

#include"cstdio"
#include"cmath"
#include"cstring"
#include"algorithm"
#include"iostream"
#include"queue"
#define MAXN 25
using namespace std;
int len[MAXN],num,ave,vis[MAXN],ok;
bool dfs(int l,int cnt,int cur)
{   if(l==ave){
        cnt++;
        l=0;
        cur=0;
        if(cnt==3) return true;
    }
    for(int i=cur;i<num;i++){
        if(!vis[i]&&len[i]+l<=ave){
            vis[i]=1;
            if(dfs(len[i]+l,cnt,i+1)) return true;
            vis[i]=0;
        }
    }
    return false;
}
int main()
{   int n;
    scanf("%d",&n);
    while(n--){
        scanf("%d",&num);
        int temp=0;
        for(int i=0;i<num;i++)
        {   scanf("%lld",&len[i]);
            temp=temp+len[i];
        }
        sort(len,len+num);
        memset(vis,0,sizeof(vis));
        if(temp%4) printf("no\n");
        else{
            ave=temp/4;
            if(dfs(0,0,0)) printf("yes\n");
            else printf("no\n");
        }
    }
    return 0;
}
时间: 2024-10-12 18:54:56

POJ 2362:Square(DFS)的相关文章

DFS POJ 2362 Square

题目传送门 1 /* 2 DFS:问能否用小棍子组成一个正方形 3 剪枝有3:长的不灵活,先考虑:若根本构不成正方形,直接no:若第一根比边长长,no 4 这题是POJ_1011的精简版:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <map> 10 #include <set> 11 #include <cmath>

POJ 2362 Square

题意:给n个木棍,问能不能正好拼成一个正方形. 解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include&

POJ 2362

知识点:dfs(深度优先搜索) 题解:基本的dfs搜索判断可行性问题.一般的dfs搜索,如果不加剪枝,复杂度是指数级的,所以必须要能发掘出优秀的剪枝条件: 在本题中,一般有如下剪枝: ①:所有线段的长度之和必须为4的倍数:②:搜索之前,把所有线段按从大到小排序,因为长度越长,在拼凑时的灵活度就越低:③:当能拼凑出3根等长的线段时,第四根就无须再搜了:④:当用某一条线段无法得出可行解时,和它等长的,一样不可以: 有了以上四个剪枝条件,这题就可以过了: add:这题理解之后可以去看看poj 1011

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

[POJ 1011]Sticks(DFS剪枝)

Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original

POJ 2362:Square 觉得这才算深度搜索

Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21821   Accepted: 7624 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 nu

poj 1084 Square Destroyer dlx解重复覆盖

分析: 将问题转化为重复覆盖问题,DancingLink解决. 代码: //poj 1084 //sep9 #include <iostream> using namespace std; const int maxN=10024; const int maxL=128; int L[maxN],R[maxN],U[maxN],D[maxN]; int C[maxN],H[maxN]; int S[maxN],A[maxN],X[maxN]; bool makeup[maxL][maxL];

ACM : POJ 2676 SudoKu DFS - 数独

SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the c

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与