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

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 }

转载于:https://www.cnblogs.com/cnblogs321114287/p/4455458.html

hdu 1518 dfs+剪枝相关推荐

  1. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. Digital Square HDU - 4394 dfs + 剪枝

    一.内容 Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow ...

  3. 【HDU - 1518】Square (经典的dfs + 剪枝)

    题干: Given a set of sticks of various lengths, is it possible to join them end-to-end to form a squar ...

  4. 【HDU - 1455】Sticks (dfs + 剪枝)

    题干: George took sticks of the same length and cut them randomly until all parts became at most 50 un ...

  5. POJ2688状态压缩(可以+DFS剪枝)

    题意:       给你一个n*m的格子,然后给你一个起点,让你遍历所有的垃圾,就是终点不唯一,问你最小路径是多少? 思路:       水题,方法比较多,最省事的就是直接就一个BFS状态压缩暴搜就行 ...

  6. poj2362 DFS+剪枝

    题大致做法就是对所有小棒子长度求和sum,sum就是正方形的周长,sum/4就是边长side. 问题就转变为:这堆小棒子能否刚好组合成为4根长度均为side的大棒子 不难了解,小棒子的长度越长,其灵活 ...

  7. HDU 1518 Square

    dfs+剪枝. AC代码例如以下: ///dfs+剪枝 62MS 300K#include<iostream> #include<cstring> #include<al ...

  8. [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]

    [问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...

  9. [DFS|剪枝] leetcode 22 括号生成

    [DFS|剪枝] leetcode 22 括号生成 1.题目 题目链接 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合. 示例: 输入:n = 3 输出:[& ...

最新文章

  1. 适合小团队协作、任务管理和进度跟踪的项目管理工具
  2. 基于Android的浮动组件,可以用于应用中的新功能展示等等。
  3. 基于内容的图像检索 Database for Content-Based Image Retrieval
  4. 极致流畅体验的密码:网易云信 QoS 策略介绍 | 体验共享技术专题
  5. 一篇文章教你弄懂java CMS垃圾回收日志
  6. EJB3.0开发环境的搭建
  7. 强行删除文件 windwos10_如何彻底删除 Windows 当中的顽固文件?
  8. 雷凌linux车机ssh,OpenWRT自动挂载支持
  9. android:layout_width=0.0dip,【教程】状态栏显示网速
  10. 你不知道的JavaScript-0
  11. 直接请求接口_「软件测试教程」基于postman进行接口测试实战
  12. utilities(matlab)—— 多元函数的数值梯度
  13. [Flash开发笔记] List控件--删除指定label或data的项
  14. 阿里面试官:LongAdder和AtomicLong哪个性能更好?我有点懵~
  15. (翻译)Importing models-FBX Importer - Animations Tab
  16. C语言程序设计(第三版)何钦铭著 习题2-2
  17. c语言+自定义关机,[分享]c语言调用shutdown编写简单定时关机软件
  18. 基于气象站点气象数据的空间插值
  19. 农村土地确权之调查公示 —— ArcGIS中地块分布图标注设置说明[地块分布图制作]
  20. 2021秋季《数据结构》_EOJ 1063.树的双亲存储法(parent+child / parent)

热门文章

  1. vb.net与FLASH的完美结合
  2. LeetCode10. 正则表达式匹配
  3. Python安装与库导入
  4. dataframe保存为txt_Word,PDF,PPT,TXT之间的相互转换方法
  5. 今日恐慌与贪婪指数为65 等级转为贪婪
  6. SAP License:SAP从业者的收入状况
  7. 机器学习算法数学基础之 —— 线性代数篇(2)
  8. 智慧发电厂+智能发电厂web端平台管理系统+Axure高保真智慧电厂系统+能耗管理+告警管理+生产监控+安防设备管理+运维设备管理+监控面板+系统管理+智慧电厂+电厂系统+axure源文件+rp原型
  9. run (牛客多校第二场)计数DP
  10. 228 Summary Ranges 汇总区间