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. JS点击事件的重叠处理(多个点击事件出现冲突)

    最近开发遇见了一个这个样的情况,一个button在一个div中,点击buton是一个事件,点击大的div也是一个事件,但是由于button在div中,点击button会把两个事件都执行了,但是我们想点 ...

  2. 关于更新SQLserver统计信息的存储过程

    1.建立t_mon_table_stat 用于存过需要更新统计信息的表 2.查找需要更新统计信息的表 insert into t_mon_table_stat SELECT DISTINCT SP.r ...

  3. Rewrite HTTP to HTTPS in Nginx

    1.推荐配置 server { listen 80; server_name example1.com example2.com; return 301 https://$host$request_u ...

  4. Android Studio 升级为3.1 踩到的坑

    原文:https://blog.csdn.net/xiariluoxue/article/details/80050700 AndroidStudio.gradle.buildToolsVersion ...

  5. swift中Double转String

    swift上手有好几天了.发现swift除了本身的几个基本类型转换,一些比较特殊的数值类型转换需要“桥接”到Objective-C来进行- 代码当然也很简单- var numString = &quo ...

  6. spring boot配置文件中 spring.mvc.static-path-pattern 配置项

    spring boot项目中的静态资源文件存放在static文件下面,当通过浏览器访问这些静态文件时,发现必须要添加static作为前缀才能访问,折腾了一番后发现,这个前缀跟 spring.mvc.s ...

  7. LoadRunner 参数化之 连接数据库进行参数化

    LoadRunner 参数化之 连接数据库进行参数化 Loadrunner(简称“LR”)对性能测试的脚本进行参数化时,由于数据量偏大,大家往往都会把数据录入到数据库表里,然后关联到LR,本文将详细介 ...

  8. Codeforces Round #248 (Div. 1) C - Tachibana Kanade's Tofu AC自动机

    C - Tachibana Kanade's Tofu 思路:把 n 个串丢进AC自动机中,然后dp就好啦. 我的代码居然是在CF上跑最快的.. #include<bits/stdc++.h&g ...

  9. 微信公众号第三方平台生成自定义菜单提示 获取"access_token失败"

    在微信公众号第三方平台要生成自定义菜单时,程序反应很慢,最终提示"获取access_token失败"(之前程序无改动,使用时间已久),查了大半天,找不出原因. 排除.在微信公众号平 ...

  10. 2017-2018-1 20179202《Linux内核原理与分析》第八周作业

    一 .可执行程序的装载 1. 预处理.编译.链接 gcc –e –o hello.cpp hello.c //预处理 gcc -x cpp-output -S -o hello.s hello.cpp ...