Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8900    Accepted Submission(s): 2893
Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
 
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
 
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
 
Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
 
Sample Output
yes
no
yes
 
Source
 

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm> #define maxn 22 int L[maxn], n, tar, times;
bool vis[maxn], ok; bool DFS(int k, int leftLen) {
if(!leftLen) {
if(++times == 4) return true;
for(int i = 1; i < n; ++i) {
if(!vis[i]) {
vis[i] = 1;
if(DFS(i + 1, tar - L[i]))
return true;
else {
--times;
vis[i] = 0;
return false;
}
}
}
} int i;
for(i = k; i < n; ++i) {
if(!vis[i] && L[i] <= leftLen) {
vis[i] = 1;
if(L[i-1] == L[i] && !vis[i-1]) {
vis[i] = 0;
continue;
}
if(DFS(i+1, leftLen - L[i]))
return true;
vis[i] = 0;
}
} return false;
} int main() {
int t, i;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
tar = 0;
for(i = 0; i < n; ++i) {
scanf("%d", &L[i]);
vis[i] = 0; tar += L[i];
} if(tar % 4) {
printf("no\n");
continue;
}
tar /= 4; std::sort(L, L + n, std::greater<int>());
if(L[0] > tar) {
printf("no\n");
continue;
} times = 0; vis[0] = 1;
DFS(1, tar - L[0]); printf(times == 4 ? "yes\n" : "no\n");
}
return 0;
}

HDU1518 Square 【剪枝】的更多相关文章

  1. HDU1518 Square(DFS,剪枝是关键呀)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  2. HDU1518 Square(DFS)

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

    Square Problem Description Given a set of sticks of various lengths, is it possible to join them end ...

  4. HDU-1518 Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  5. HDU1518:Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  6. HDU1518 Square

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

  7. 1317: Square(DFS+剪枝)

    Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form ...

  8. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  9. 【DFS+剪枝】Square

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...

随机推荐

  1. nginx php mysql日志配置

    1.编辑mysql的配置文件my.cnf,这个文件通常在/etc目录下,但我用rpm装mysql的时候这个配置文件是在/usr目录下,但我测试过,无论是放在/etc目录下,还是放在     /usr目 ...

  2. php-fpm进程管理方式(static和dynamic)

    目前最新5.3.x的php-fpm,有两种管理进程的方式,分别是static和dynamic. 如果设置成static,进程数自始至终都是pm.max_children指定的数量,pm.start_s ...

  3. **PHP转义Json里的特殊字符的函数

    http://www.banghui.org/11332.html 在给一个 App 做 API,从服务器端的 MySQL 取出数据,然后生成 JSON.数据中有个字段叫 content,里面保存了文 ...

  4. 国际混淆C代码大赛获奖作品部分源码

    国际C语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从1984年开始,每年举办一次(1997年.1999年 ...

  5. Web前端开发最佳实践(8):还没有给CSS样式排序?其实你可以更专业一些

    前言 CSS样式排序是指按照一定的规则排列CSS样式属性的定义,排序并不会影响CSS样式的功能和性能,只是让代码看起来更加整洁.CSS代码的逻辑性并不强,一般的开发者写CSS样式也很随意,所以如果不借 ...

  6. echarts3.0 本期累计堆叠

    @{ ViewBag.Title = "barlj"; } <h2>barlj</h2> <div id="main" style ...

  7. Netty 实现SSL安全连接(wss://)

    原文:Netty5使用自签证书实现SSL安全连接 在客户端是https:// 协议下,使用ws:// 协议连接会报错的,得需要使用wss:// 连接.(ip连接失败时使用域名连接) netty创建服务 ...

  8. poj1182 食物链(带权并查集)

    题目链接 http://poj.org/problem?id=1182 思路 前面做的带权并查集的权值记录该结点与其父结点是否是同一类,只有两种取值情况(0,1),在这题中某结点a和其父结点b的取值共 ...

  9. modCount干嘛的

    在ArrayList.LinkedList.HashMap等等的内部增删改中我们总能看到modCount的身影,modCount字面意思就是修改次数,但为什么要记录modCount的修改次数呢? 大家 ...

  10. Git 码云操作

    https://www.cnblogs.com/lanxiang/p/7487971.html 将项目提交到码云 在码云创建项目 然后在项目目录下: 打开后输入: git init 目录下出现.git ...