HDU 1518 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int q[22];
int vis[22];
int cmp(int a,int b)
{
return a<b;
}
int t,n,s;
void dfs(int num,int k,int length) //num 是已完毕的边 k是 数组的位置 length是当前边的长度
{
if(t==1)
return ;
if(num==4)
{
t=1;
return ;
}
if(length==s)
{
dfs(num+1,0,0);
if(t==1)
return ;
}
for(int i=k; i<n; i++)
{
if(length+q[i]<=s&&vis[i]==0)
{
vis[i]=1;
dfs(num,i+1,length+q[i]);
vis[i]=0;
}
}
}
int main()
{
int m;
scanf("%d",&m);
while(m--)
{
t=0;
s=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&q[i]);
s=s+q[i];
}
if(s%4!=0)
{
printf("no\n");
continue;
}
sort(q,q+n,cmp);
s=s/4;
if(q[n-1]>s)
{
printf("no\n");
continue;
}
dfs(0,0,0);
if(t==1)
printf("yes\n");
else
printf("no\n"); }
return 0;
}
标准的回溯 可惜 我没有看到这个问题 要运行数据
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 1518 Square 搜索的更多相关文章
- hdu 1518 Square(深搜+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...
- hdu 1518 Square 深搜,,,,花样剪枝啊!!!
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- hdu 1518 Square 木棍建正方形【DFS】
题目链接 题目大意: 题意就是输入棍子的数量和每根棍子的长度,看能不能拼成正方形. #include <bits/stdc++.h> using namespace std; int n, ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU 1518 Square
解题思路:sum%4!=0 , max<sum/4 #include<iostream>#include<cstdio>#include<cstring> ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- 杭电1518 Square(构成正方形) 搜索
HDOJ1518 Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1518 拼正方形
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...
- hdu 5468(莫比乌斯+搜索)
hdu 5468 Puzzled Elena /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5 Sample Output Case #1: ...
随机推荐
- SVN的switch命令
语法就不说了,文档有的是,主要是两个常用的用法: . 切换资源库(svn sw --relocate) [plain] view plaincopy svn sw --relocate <fro ...
- GString及IntelliJIdea中调试Groovy的操作步骤
今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...
- vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式
vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式 #Region "WPF 当浏览器窗体关闭 ...
- C#区域截图——调用API截图
原文:C#区域截图——调用API截图 前言:截图对于一个C++开发者来说无非是小菜一碟,也有朋友使用C#的 Graphics.CopyFromScreen 方法屏幕操作,作为一名整天想着用 C++ 开 ...
- java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor An ExecutorService that executes each submitted task using o ...
- .Net程序猿玩转Android开发---(7)相对布局RelativeLayout
相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行 ...
- Thinkpad X200 屏幕备案
妈妈蛋,屏幕废物前几天(闪屏->暗->变暗),因此,它只能监视房外 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk2NTg5MA= ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- Add/Remove listview web part in publish site via powershell
1. Here is the code: Add WebPart in Publish Site Example : AddWebPartPublish http://localhost " ...
- Oracle SQL Lesson (11) - 创建其他数据库对象(试图/序列/索引/同义词)
schema(模式)一个用户下一组对象的集合,一般与用户名一致. 视图 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias].. ...