HDU1518 Square 【剪枝】
Square
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
yes
no
yes
#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 【剪枝】的更多相关文章
- HDU1518 Square(DFS,剪枝是关键呀)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- HDU1518 Square(DFS)
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 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 ...
- HDU-1518 Square(DFS)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- HDU1518:Square(DFS)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- HDU1518 Square
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...
- 1317: Square(DFS+剪枝)
Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form ...
- hdu 1518 Square(深搜+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...
- 【DFS+剪枝】Square
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...
随机推荐
- 网络路径查询traceroute
Traceroute用法 网友:适兕 发布于: 2006.08.24 08:14 (共有条评论) 查看评论 | 我要评论 一.什么是Traceroute? In ...
- JS中精选this关键字的指向规律你记住了吗
1.首先要明确: 谁最终调用函数,this指向谁 this指向的永远只可能是对象!!!!! this指向谁永远不取决于this写在哪,而取 ...
- C++链接与装载
1..obj文件的内部结构 2.映射到进程虚拟空间 3.链接的原理 C++ Code 123456789 1.未解决符号表:提供了所有在该编译单元里引用但是定义并不在本编译单元里的符号及其 ...
- shell 中>/dev/null 2>&1含义
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 分解这个组合:“>/dev/null 2>&1” 为五 ...
- 首次加载进来DEV控件列表第一行颜色总是不对,后台代码显示的数据正确
1:行改变的颜色正确的颜色: 1.1颜色效果如下图: 1.2:设置行改变颜色: 2:结果首次加载第一行颜色为: 3:解决方案: 3.1 :Views-->OptionsSelection --& ...
- HTML5练习2
1.邮箱注册网页 主要代码: <!doctype html> <html> <meta charset="utf-8"> <title&g ...
- Linux--忘记MySQL密码的解决方法和输入mysqld_safe --skip-grant-tables &后无法进入MySQL的解决方法
https://blog.csdn.net/qq_35389417/article/details/78910974
- 程序设计实习MOOC / 程序设计与算法(一)第二周测验(2018春季)
编程题: 1:对齐输出 总时间限制: 1000ms 内存限制: 65536kB 描述 读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们. 输入 只有一行,包含三个整数,整数之间以一个空格分 ...
- jenkins发布maven项目
(1)环境介绍 (2)配置ssh配置:系统管理--->系统设置 做这样的配置是方便打包之后war包或jar包复制到/tomcat/update目录下 (3)安装git 1丶不要使用1.8版本以下 ...
- MongoDB入门教程三[数据类型]
MongoDB的文档使用BSON(Binary JSON)来组织数据,BSON类似于JSON,JSON只是一种简单的表示数据的方式,只包含了6种数据类型(null.布尔.数字.字符串.数组及对象),不 ...