题目传送门

 /*
DFS:问能否用小棍子组成一个正方形
剪枝有3:长的不灵活,先考虑;若根本构不成正方形,直接no;若第一根比边长长,no
这题是POJ_1011的精简版:)
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int a[MAXN];
bool vis[MAXN];
int len, sum;
int n, m; bool cmp(int x, int y)
{
return x > y;
} bool DFS(int ans, int cnt, int s)
{
if (cnt == )
{
return true;
} for (int i=s; i<=m; ++i)
{
if (!vis[i] && ans + a[i] <= len)
{
vis[i] = true;
if (ans + a[i] == len)
{
if (DFS (, cnt + , ) == true) return true;
vis[i] = false;
}
else
{
if (DFS (ans + a[i], cnt, i) == true) return true;
vis[i] = false;
}
}
} return false;
} int main(void) //POJ 2362 Square
{
//freopen ("POJ_2362.in", "r", stdin); scanf ("%d", &n);
while (n--)
{
sum = ;
memset (vis, , sizeof (vis)); scanf ("%d", &m);
for (int i=; i<=m; ++i)
{
scanf ("%d", &a[i]);
sum += a[i];
}
sort (a+, a++m, cmp); //Cut 1 if (m < || sum % != ) //Cut 2
{
puts ("no"); continue;
}
len = sum / ; if (a[] > len) //Cut 3
{
puts ("no"); continue;
} if (DFS (, , ) == true) puts ("yes");
else puts ("no");
} return ;
} /*
yes
no
yes
*/

DFS POJ 2362 Square的更多相关文章

  1. POJ 2362 Square DFS

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

  2. POJ 2362 Square

    题意:给n个木棍,问能不能正好拼成一个正方形. 解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ 代码: #include<stdio.h> # ...

  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. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

  6. [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

    题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory L ...

  7. DFS POJ 3087 Shuffle'm Up

    题目传送门 /* 题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子 DFS:直接模拟搜索,用map记录该字符串是否被搜过.读懂题目是关键. */ ...

  8. DFS POJ 1321 棋盘问题

    题目传送门 /* DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 */ #include <cstdio> #include <algori ...

  9. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

随机推荐

  1. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  2. onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  3. HTML5 自制本地网页视频播放器

    HTML5初试:本地视频用网页打开啦半个广告都可以没有,看来暴风什么的快要淘汰了. 视频格式还是有要求的,看来要备一个转码器. 格式 IE Firefox Opera Chrome Safari Og ...

  4. centos安装redis及php-redis扩展

    centos安装redis及php-redis扩展  Linux, WEB 七162012 今天公司同事要求在测试机上安装redis,并且要求让php安装上redis的扩展,redis是一个key-v ...

  5. centos安装ssdb

    在编译之前要下gcc编译器 yum -y install gcc*   编译和安装 wget --no-check-certificate https://github.com/ideawu/ssdb ...

  6. 【OpenStack】OpenStack系列13之Nova源码解析与API扩展

    学习思路 议程:代码结构-主干流程-分层架构-业务模型-数据库模型-消息模型 分布式架构:Api:横向扩展    rpc:纵向扩展 分层架构:Controller接口层.View/Manager逻辑层 ...

  7. Cannot change version of project facet Dynamic web module to 2.5

    在工程目录下有一个.settings文件夹,打开org.eclipse.wst.common.project.facet.core.xml做如下修改:<installed facet=" ...

  8. kettle使用log4j管理输出日志

    在使用kettle进行数据分析和清洗时日志非常多而且杂乱,使用原有的日志有时找不到异常的位置,有时日志不够详细,说简单一点就是日志不是我们想要的.因而对kettle日志进行相应的管理就想得尤为重要了. ...

  9. (原创)Python文件与文件系统系列(5)——stat模块

    stat模块中定义了许多的常量和函数,可以帮助解释 os.stat().os.fstat().os.lstat()等函数返回的 st_result 类型的对象. 通常使用 os.path.is*() ...

  10. 【python】队列

    来源:http://www.cnblogs.com/yupeng/p/3413852.html 关于队列 >>> from collections import deque > ...