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. mac idea内存溢出

    VM options: -mx2048m -XX:MaxPermSize=2048m -Drebel.spring_plugin=true -Drebel.hibernate_plugin=true

  2. mysql数据库主从同步复制原理

    MySQL的Replication(英文为复制)是一个多MySQL数据库做主从同步的方案,特点是异步复制,广泛用在各种对MySQL有更高性能.更高可靠性要求的场合.与之对应的是另一个同步技术是MySQ ...

  3. sql server 约束 查找

    --1.主键约束 SELECT tab.name AS [表名], idx.name AS [主键名称], col.name AS [主键列名] FROM sys.indexes idx JOIN s ...

  4. Linux的bash快捷键

    Ctrl-A 相当于HOME键,用于将光标定位到本行最前面 Ctrl-E 相当于End键,即将光标移动到本行末尾 Ctrl-B 相当于左箭头键,用于将光标向左移动一格 Ctrl-F 相当于右箭头键,用 ...

  5. USACO 4.4 Shuttle Puzzle

    Shuttle PuzzleTraditional The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, ...

  6. python和redis简单交互

    python和redis简单交互 1.安装redis模块 pip3 install redis 2.redis模块简单使用: # /usr/bin/env python3 import redis c ...

  7. 8-5 Wine trading in Gergovia Gergovia的酒交易 uva11054

    等价转换思维题 题意: 直线上有n(2<=n<=100000)个等距的村庄  每个村庄要么买酒 要么卖酒  设第i个村庄对酒的需求量为ai  绝对值小于一千  其中ai大于0表示买酒   ...

  8. thinkphp中导入和使用阿里云OSSsdk

    照做绝对行,在ThinkPHP中,第三方库都放在ThinkPHP/Library/Vendor/路径下. 1.下载OSS PHP SDK:https://help.aliyun.com/documen ...

  9. html+css制作五环(代码极简)

    五环 把五环做成一个浮动,总是位于屏幕中央的效果. 难点 定位的练习 position :fixed 位于body中间的时候 left:50%:top:50%; css内部样式表的使用 style=& ...

  10. Pearls in a Row CodeForces 620C 水题

    题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...