博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1518 Square 【剪枝】
阅读量:4322 次
发布时间:2019-06-06

本文共 2247 字,大约阅读时间需要 7 分钟。

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 
#include
#include
#include
#define maxn 22int 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
()); 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;}

转载于:https://www.cnblogs.com/bhlsheji/p/5386613.html

你可能感兴趣的文章
MTK android 设置里 "关于手机" 信息参数修改
查看>>
单变量微积分笔记6——线性近似和二阶近似
查看>>
补几天前的读书笔记
查看>>
HDU 1829/POJ 2492 A Bug's Life
查看>>
CKplayer:视频推荐和分享插件设置
查看>>
CentOS系统将UTC时间修改为CST时间
查看>>
redis常见面试题
查看>>
导航控制器的出栈
查看>>
玩转CSS3,嗨翻WEB前端,CSS3伪类元素详解/深入浅出[原创][5+3时代]
查看>>
iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
查看>>
Delphi消息小记
查看>>
HNOI2016
查看>>
JVM介绍
查看>>
将PHP数组输出为HTML表格
查看>>
Java中的线程Thread方法之---suspend()和resume() 分类: ...
查看>>
经典排序算法回顾:选择排序,快速排序
查看>>
BZOJ2213 [Poi2011]Difference 【乱搞】
查看>>
c# 对加密的MP4文件进行解密
查看>>
AOP面向切面编程C#实例
查看>>
Win form碎知识点
查看>>