HDU - 1518 :Square

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
InputThe 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.
OutputFor 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

组成正方形的题,时间给的比较多,不需要过多的剪枝,直接暴力DFS就过了。
 1 #include<iostream>
 2 #include<string.h>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 bool flag;
 8
 9 int a[25],t,sum,b[25];
10
11
12 void DFS(int x,int y,int z)
13 {
14     if(flag)
15         return;
16     if(x==4)
17         flag=true;
18     if(flag)
19         return;
20     if(y==sum/4)
21         DFS(x+1,0,0);
22     if(flag)
23         return ;
24     for(int i=z;i<t;i++)
25     {
26         if(!b[i]&&y+a[i]<=sum/4)
27         {
28             b[i]=1;
29             DFS(x,y+a[i],i+1);
30             b[i]=0;
31             if(flag)
32                 return ;
33
34         }
35     }
36 }
37
38 int main()
39 {
40     int n;
41     cin>>n;
42     while(n--)
43     {
44         cin>>t;
45         flag=false;
46         memset(a,0,sizeof(a));
47         memset(b,0,sizeof(b));
48         int f=1,max=0;
49         sum=0;
50         for(int i=0;i<t;i++)
51         {
52             cin>>a[i];
53             sum+=a[i];
54             if(a[i]>max)
55                 max=a[i];
56         }
57         if(sum%4||max>sum/4)
58             flag=false;
59             else
60             {
61                 sort(a,a+t);
62                 DFS(0,0,0);
63             }
64         if(flag)
65             cout<<"yes"<<endl;
66             else
67                 cout<<"no"<<endl;
68
69     }
70
71     return 0;
72 }
时间: 2024-10-26 06:11:24

HDU - 1518 :Square的相关文章

hdu 1518 Square(深搜dfs)

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

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搜索可参考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

HDU 1241 :Oil Deposits

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11360    Accepted Submission(s): 6626 Problem Description The GeoSurvComp geologic survey company is responsible for detecting under

HDU 5618:Jam&#39;s problem again(CDQ分治+树状数组处理三维偏序)

http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:-- 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #d

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu