hdu 1518 dfs+剪枝

题目大意:
几根棒子能否组成一个正方形

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

虽然不用pos直接从0开始枚举也可以有答案,但会超时,加个pos,以前dfs过的情况就不会再出现了,想起以前bc的一道题也是这样

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000007
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 #define cl(a) memset(a,0,sizeof(a))
13 #define ts printf("*****\n");
14 const int MAXN=1005;
15 int n,m,tt;
16 int a[30];
17 bool vis[30];
18 int len1;
19 bool dfs(int len,int pos,int tot)
20 {
21     if(tot==4)  return 1;
22     for(int i=pos;i<n;i++)
23     {
24         if(vis[i])  continue;
25         if(len+a[i]==len1)
26         {
27             vis[i]=1;
28             if(dfs(0,0,tot+1))  return 1;
29             vis[i]=0;
30         }
31         if(len+a[i]<len1)
32         {
33             vis[i]=1;
34             if(dfs(len+a[i],i,tot))   return 1;
35             vis[i]=0;
36         }
37     }
38     return 0;
39 }
40 int main()
41 {
42     int i,j,k;
43     #ifndef ONLINE_JUDGE
44     freopen("1.in","r",stdin);
45     #endif
46     scanf("%d",&tt);
47     while(tt--)
48     {
49         scanf("%d",&n);
50         len1=0;
51         for(i=0;i<n;i++)
52         {
53             scanf("%d",&a[i]);
54             len1+=a[i];
55         }
56         if(len1%4!=0||n<4)      //数目小于4或者总长不能被4除
57         {
58             printf("no\n");
59             continue;
60         }
61         len1/=4;
62         sort(a,a+n);
63         memset(vis,0,sizeof(vis));
64         if(dfs(0,0,0))  printf("yes\n");
65         else printf("no\n");
66     }
67 }
时间: 2024-08-07 00:17:11

hdu 1518 dfs+剪枝的相关文章

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

hdu 1518(dfs)

参考页面: http://www.yuanjiaocheng.net/CSharp/Csharp-keys.html http://www.yuanjiaocheng.net/CSharp/csharp-interface.html http://www.yuanjiaocheng.net/CSharp/Csharp-operators.html http://www.yuanjiaocheng.net/CSharp/Csharp-if-else.html http://www.yuanjiao

hdu 1584 dfs+剪枝

蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3730    Accepted Submission(s): 1591 Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也

hdu 1728 DFS+剪枝 逃离迷宫

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 typedef struct Node 6 { 7 int x, y; 8 }Node; 9 10 const int MAX = 10000; 11 const int N = 110; 12 const int dir[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1} };//移动方向 13 bool

hdu 1010 dfs+剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 119601 Accepted Submission(s): 32313 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot.

hdu 1445 dfs剪枝

题意比较简单:重点在剪枝上. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int n,num[70],mark[70],k,flash; int dfs(int s,int ii,int t,int c) { if(t==c) { flash=1; return 1; } if(flash) return 0;

HDU 5113 dfs剪枝

题意:告诉格子规格,颜色个数,以及每个颜色能涂得格子数目,问是否能够实现相邻两个格子的颜色数目不相同. 分析:因为数据很小,格子最多是5 * 5大小的,因此可以dfs.TLE了一次之后开始剪枝,31ms过.剪枝看代码. 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include &

hdu 5305 dfs+剪枝

思路:对每一条边涂上颜色1或-1,颜色值加到关联的两个点上,则一种成功的方案必须满足最后每个点的值为0. 剪枝:统计出和某个点i相关联的边的个数,如果枚举到某一条边的时候发现:abs(sum[i]) > degree[i],则剪去,其中sun[i]表示i点的值,degree[i]表示剩下的还没有枚举的和i关联的边的个数. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using n

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]