题意:给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. HDU 1882 Strange Billboard(位运算)

    题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第 ...

  2. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  3. 用static关键字修饰类

    Java里面static一般用来修饰成员变量或函数.但有一种特殊用法是用static修饰内部类,普通类是不允许声明为静态的,只有内部类才可以.被static修饰的内部类可以直接作为一个普通类来使用,而 ...

  4. Windows下gcc以及Qt的DLL文件调用之总结(三种方法)

    DLL与LIB的区别 :1.DLL是一个完整程序,其已经经过链接,即不存在同名引用,且有导出表,与导入表lib是一个代码集(也叫函数集)他没有链接,所以lib有冗余,当两个lib相链接时地址会重新建立 ...

  5. VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)

    原文地址: VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)-软件开发-鸡啄米 http://www.jizhuomi.com/software/139.html   上一讲中鸡 ...

  6. (3)TXT转为XML

    <?xml version="1.0" encoding="utf-8"?> <bocb2e> <head /> <t ...

  7. Space and GridLayout 教程

    Ice Cream Sandwich (ICS) sports two new widgets that have been designed to support the richer user i ...

  8. JS中字符串拼装 单双引号的处理 字符转义

    js中可能会用到动态追加元素,可能数据也是从后台传过来的,当然有两种思路, 1.在后台拼装好直接返回; 2.在前台js里面拼装, 如果拼装大量的html时可能单双引号就容易出问题;那么如何解决呢?最近 ...

  9. oracle .bash_profile

    [oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...

  10. Android View 绘制过程

    Android的View绘制是从根节点(Activity是DecorView)开始,他是一个自上而下的过程.View的绘制经历三个过程:Measure.Layout.Draw.基本流程如下图: per ...