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. jersey HTTP Status 400 - Bad Request

    原因是jersey 内置的转换器,只能做简单的类型转换如: 首先客户端提交上来的一定是String; String ----> String/Long/Boolean 这些基本的 可以转换,但是 ...

  2. JAVA复习笔记之GC部分

       前言:垃圾回收机制,大家都知道JAVA的垃圾回收都是JVM自动回收的,不需要程序员去管理.但是我们还是得知道原理才能在适当时机进行JVM调优 原理:当我们new 一个对象时JVM堆区就会分配一块 ...

  3. springMVC源码分析--HttpMessageConverter参数read操作(二)

    上一篇博客springMVC源码分析--HttpMessageConverter数据转化(一)中我们简单介绍了一下HttpMessageConverter接口提供的几个方法,主要有以下几个方法: (1 ...

  4. TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?

    TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小.TreeMap要求存放的键值对映射的键必 ...

  5. 安装配置SVN

    官网下载地址,下载完之后如果想看到中文,可以下载语言包进行安装,安装之后TortoiseSVN -> Settings -> General -> Language选项中选择:中文( ...

  6. SRILM语言模型格式解读

    先看一下语言模型的输出格式 \data\ ngram = ngram = ngram = \-grams: -5.24036 'cause -0.2084827 -4.675221 'em -0.22 ...

  7. js获取光标位置并插入内容

    先来几个网上找的参考资源,我爱互联网,互联网使我变得更加强大. https://blog.csdn.net/mafan121/article/details/78519348 详细篇,该作者很用心的解 ...

  8. LoadRunner中文乱码问题解决方法

    LoadRunner中文乱码问题解决方法 前段时间在录制,增强,整合LoadRunner脚本,期间两次遇到了中文乱码问题.在此记录一下中文乱码问题的解决办法. 一.录制回放中文乱码 我录制登陆的脚本, ...

  9. Java学习笔记之:Struts2.0 环境搭建

    一.介绍 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互. 二 ...

  10. 百度地图API--Key的获得

    [开年后花了半个月的时间学习了百度地图API开发,准备投入项目中,学习的过程中写了一些简单的总结,在部门内部做了一个简单的分享培训,这里希望将自己的仅有的一点点关于百度地图API的收获分享给社区,整个 ...