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];
 7 int l,n;
 8 int dfs(int len,int gen,int iqq)
 9 {
10     if (gen==3)
11         return 1;
12     for(int i=iqq; i<n; i++)
13     {
14         //cout<<visit[len]<<endl;
15         if (!visit[i])
16         {
17             visit[i]=1;
18             if (len+ap[i]==l)
19             {
20                 //cout<<len<<endl;
21                 if(dfs(0,gen+1,0))
22                     return 1;
23             }
24             else if (len+ap[i]<l)
25             {
26                 //cout<<len<<endl;
27                 if(dfs(len+ap[i],gen,i)) return 1;
28             }
29             visit[i]=0;
30         }
31     }
32     return 0;
33 }
34 int main ()
35 {
36     int t,sum;
37     while (cin>>t)
38     {
39         while (t--)
40         {
41             cin>>n;
42             sum=0;
43             //Max=0;
44             for (int i=0; i<n; i++)
45             {
46                 cin>>ap[i];
47                 sum+=ap[i];
48             }
49             memset(visit,0,sizeof(visit));
50             sort(ap,ap+n);
51             if (sum%4==0&&n>=4&&ap[n-1]<=sum/4)
52             {
53
54                 l=sum/4;
55                 if (dfs(0,0,0))
56                     printf ("yes\n");
57                 else
58                     printf ("no\n");
59                 //cout<<n<<endl;
60             }
61             else printf ("no\n");
62         }
63     }
64 }

hdu 1518 Square(深搜+剪枝)

时间: 2024-10-24 23:07:27

hdu 1518 Square(深搜+剪枝)的相关文章

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

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(深搜dfs)

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

UVA1374 - Power Calculus(迭代深搜+剪枝)

题目链接 题意:给出x和正整数n,问最少需要几次乘除法 可以得到n = x^m 思路:其实是关于指数的操作,即从1到m最少的步数.我们可以先确定最少步数m,然后进行迭代,迭代的过程也就是判断通过相加减所得到的数可以在m次操作中等于n,如果符合,m即为最小步数,如果不符合,m++,进行下一次迭代.迭代过程中要注意剪枝,即剩余的次数如果每次都是取最大值相加还是比n小的话,就直接跳出. 代码: #include <iostream> #include <cstdio> #include

poj1011(深搜+剪枝)

题意:给m根木棍,将它们重新拼成n根一样长的木棍,并使得n尽量大(即每个新木棍尽量短). 解法:经典的搜索题目.从小到大枚举拼成的新木棍长度,每次枚举进行一次深搜.这题关键是如何剪枝. 1.当枚举的长度不能整除总长度的时候,剪枝:(这个很显然) 2.先将木棍从长到短排序,枚举时先尝试长的木棍.(先枚举长的可以使得搜索深度不至于过深) 3.深搜时,不要企图通过换掉一个新木棍的第一根来改变失败的局面(换掉第一根A,那么A也会在以后的新木棍中被使用,但是已经证明了A无法拼成了,所以不必再尝试下去了)

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 搜索

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

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 1455 sticks(经典深搜+剪枝技巧)

 题意:有一堆的木棒,长度不一,它们是有一些整齐的木棒截断而成的,求最小的木棒原始长度. 思路很简单深搜,但是直接深搜的话会tle,首先可以对木棒长度进行排序从大到小,优先使用长度长的木棒,加入当前长度不符合,考虑下一个木棒 其次如果长度为零的时候选择木棒失败,那么直接退出,实测加上这一剪枝就可以ac,这一剪枝可以帮助我们尽可能的在靠近树根处剪枝,所以优化效果很明显. 然后是如果这次选择的木棒长度和上次失败时的一样,那么剪枝. #include<cstdio> #include<cs