题意:给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. PHP Zend Studio9.0怎么把代码搞成和服务器端的同步(就是直接在服务器端修改)

    Zend Studio 可以直接通过Remote System的方式直接连接服务器端的代码,就是可以直接修改服务器端的代码,不过修改的时间小心点,修改就会立即生效的. 选择Remote Systems ...

  2. mac上eclipse上配置hadoop

    在mac上安装了eclipse之后,配置hadoop其实跟在linux上配置差不多,只是mac上得eclipse和界面和linux上得有点不同. 一:安装eclipse eclipse得安装比较简单, ...

  3. JsRender系列demo(7)compline

    <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery.j ...

  4. Android 使用MediaRecorder录音

    package com.example.HyyRecord; import android.app.Activity; import android.content.Intent; import an ...

  5. VirtualBox 给虚拟机绑定IP

    在VirtualBox中,有时候打开虚拟机,会出现Waiting for 60 seconds more for network configuration这种情况,从而使得开机变得很慢. 通过以下操 ...

  6. Netty 的Downstream 和 Upstream

    Netty的Downstream 和 Upstream 如果一个event从第一个handler传递直到最后一个handler就是 Upstream 相反的如果一个event从最后一个handler传 ...

  7. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  8. javaWEB邮件测试

    新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...

  9. apache配置优化

    最近参加了很多面试,多多少少有点小感悟,可以说观念转变了不少,特别是对于作为一个开发人员的定位,原来只是认为开发人员就只需要写好代码就行了,所以只需要有数据结构,算法,设计模式,重构方面的知识就行了. ...

  10. Java学习笔记之:Java简介

    一.引言 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出. ...