题意:给n个木棍,问能不能正好拼成一个正方形。

解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; int n;
int stick[25];
bool vis[25];
bool dfs(int len, int remlen, int pos, int num)
{
if(remlen == 0)
{
if(num == 3) return true;
return dfs(len, len, 0, num + 1);
}
for(int i = pos; i < n; i++)
{
if(i > 0 && !vis[i - 1] && stick[i] == stick[i - 1]) continue;
if(!vis[i] && remlen >= stick[i])
{
vis[i] = true;
if(dfs(len, remlen - stick[i], i + 1, num)) return true;
vis[i] = false;
}
if(!vis[i] && i == 0) return false;
}
return false;
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(vis, 0, sizeof vis);
scanf("%d", &n);
int sum = 0;
int maxn = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &stick[i]);
sum += stick[i];
maxn = max(maxn, stick[i]);
}
sort(stick, stick + n, cmp);
if(sum % 4 != 0 || maxn > sum / 4 || n < 4)
{
puts("no");
continue;
}
if(dfs(sum / 4, sum / 4, 0, 1)) puts("yes");
else puts("no");
}
return 0;
}

  

POJ 2362 Square的更多相关文章

  1. DFS POJ 2362 Square

    题目传送门 /* DFS:问能否用小棍子组成一个正方形 剪枝有3:长的不灵活,先考虑:若根本构不成正方形,直接no:若第一根比边长长,no 这题是POJ_1011的精简版:) */ #include ...

  2. POJ 2362 Square DFS

    传送门:http://poj.org/problem?id=2362 题目大意: 给一些不同长度的棍棒,问是否可能组成正方形. 学习了写得很好的dfs 赶紧去玩博饼了.....晚上三个地方有约.... ...

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

    Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21821   Accepted: 7624 Descripti ...

  4. poj 2362:square

    题目大意:给你T组数据,每组数据有n个棍子,问你能不能用这些棍子拼成一个正方形(所有都要用上,而且不能截断棍子). Sample Input 34 1 1 1 15 10 20 30 40 508 1 ...

  5. POJ 1099 Square Ice

    Square Ice Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxyg ...

  6. (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。

    Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...

  7. POJ 1099 Square Ice 连蒙带猜+根据样例找规律

    目录 题面 思路 思路 AC代码 题面 Square Ice Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4526   A ...

  8. poj 2362

    回溯加剪枝 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...

  9. [DLX反复覆盖] poj 1084 Square Destroyer

    题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...

随机推荐

  1. 【QT】计时器制作

    应小伙伴的要求,做一个小计时器.功能是点击开始就从00:00:00开始计时,点击暂停就暂停计时,点击停止就停止计时. 界面如上图,使用ui设计师直接拖的.按钮和图标的图片都是网上下载的.用美图秀秀抠成 ...

  2. 核稀疏表示分类(KSRC)

    参考:<Kernel SparseRepresention-Based Classifier> 原文地址:http://www.cnblogs.com/Rosanna/p/3372153. ...

  3. Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjav

    在eclipse上运行hadoop报错:Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.ha ...

  4. hadoop集群基本配置

    最近在学习hadoop.网上具体过程很多,我就说说简单过程和注意问题. 环境:宿主机(windows64),虚拟机(centos64). 准备软件: 1.Vmware——虚拟机 2.centos镜像文 ...

  5. Linux资源监控命令/工具(综合)

    目录: ps pstree pidof top free uptime ifuser lsof mpstat vmstst pidstat iostat iotop watch sar 1.ps 1) ...

  6. React 性能调优原理

    一.React影响性能的两个地方 二.调优原理

  7. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  8. TCP和UDP协议的应用/参数查看

    TCP发送的包有序号,对方收到包后要给一个反馈,如果超过一定时间还没收到反馈就自动执行超时重发,因此TCP最大的优点是可靠.一般网页(http).邮件(SMTP).远程连接(Telnet).文件(FT ...

  9. 非常实用的PHP代码片段推荐

    当使用PHP进行开发的时候,如果你自己收 藏 了一些非常有用的方法或者代码片段,那么将会给你的开发工作带来极大的便利.今天我们将介绍10个超级好用的PHP代码片段,希望大家能够喜欢! 1.  使用te ...

  10. MariaDB10.2.X-新特性1-支持分析函数

    前言:前段时间看到MariaDB10.2出测试版本了,心想有什么新特性玩玩,大家都知道MySQL不支持分析函数,但是MariaDB10.2.X支持分析函数了, 1.表结构 CREATE TABLE ` ...