题目传送门

 /*
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. Model backing a DB Context has changed; Consider Code First Migrations

    Model增加一个字段,数据库对应的也手动添加了字段但是运行时报错 The model backing the 'TopLogDbContext' context has changed since ...

  2. RPM常用组合【转载】

    RPM常用组合 -ivh:安装显示安装进度--install--verbose--hash -Uvh:升级软件包--Update: -qpl:列出RPM软件包内的文件信息[Query Package ...

  3. OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能

    http://www.hopesoft.org/blog/?p=1188 http://www.imagemagick.org/download/ 2.用法 原始图片是input.jpg,尺寸:160 ...

  4. 一个很不错的适合PHPER们书单,推荐给大家【转】

    来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...

  5. windows2003批量添加和导出所有ip

    批量添加IP 在cmd命令行下运行: FOR /L %i IN (130,1,190) DO netsh interface ip add address "本地连接" 192.1 ...

  6. JDBC的基本步骤

    JDBC全名是Java Data Base Connectivity就是Java数据库连接,这是Java用于向数据库执行SQL语句的API,JDBC可以为多种关系型数据库提供统一的访问,而不用考虑细节 ...

  7. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. Java for LeetCode 048 Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. 【贪心】最大乘积-贪心-高精度-java

    问题 G: [贪心]最大乘积 时间限制: 1 Sec  内存限制: 128 MB提交: 34  解决: 10[提交][状态][讨论版] 题目描述  一个正整数一般可以分为几个互不相同的自然数的和,如3 ...

  10. codeforces 486B.OR in Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...