Square(hdu 1511)
题目描述:
/*要剪枝,当所有木棒总长度不能被4整除时以及木棒最大长度大于总长度除以4时,
* 不能组成正方形,直接输出no
* 深搜时从第一个开始往后搜索,只要满足当前边长+当前木棒长<正方形边长,
* 就标记该木棒,并继续搜索后面的木棒,当木棒长度=sum/4 时,count加1,
* 当count=3时表明能够成正方形,flag=1,返回,flag=0则不能组成正方形。*/
#include<iostream>
#include<cstring> int visit[];
bool flag;
int number[];
int n,len; using namespace std; void dfs(int cur,int pos,int count);
int main()
{
int m;
cin >> m;
while(m--)
{
int max = ,sum = ;
cin >> n;
memset(visit,,sizeof(visit));
for(int i = ;i < n;i++)
{
cin >> number[i];
sum += number[i];
if(number[i] > max)
max = number[i];
}
len = sum / ;
if(sum % != || max > len)//不能总和不能被4整除或最大值大于平均值
{
cout << "no" << endl;
continue;
}
flag = ;
dfs(,,);
if(flag)
cout << "yes" << endl;
else
cout << "no" << endl;
}
} void dfs(int cur,int pos,int count)
{
if(cur == len)//木棍相加的值等于平均值
{
count++;
cur = ;//当一边成立时,要考虑另外一边注意将cur清0
pos = ;
if(count == )//当有三边成立时
{
flag = ;
return;
}
}
for(int i = pos;i < n;i++)
{
if(!visit[i])//还没有被访问过
{
if((cur + number[i]) <= len)//加上木棍后,长度小于或等于平均值
{
visit[i] = ;
dfs(cur + number[i],i,count);
if(flag)//一旦有成立的,跳过剩下的判断
return;
visit[i] = ;//回溯
}
}
}
}
代码分析:
这道题目要注意的地方就是剪枝。
参考地址:http://www.cnblogs.com/PegasusWang/archive/2013/04/08/3008942.html
Square(hdu 1511)的更多相关文章
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- hdu 5079 Square
http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,… ...
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- HDU 6125 - Free from square | 2017 Multi-University Training Contest 7
思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1398 Square Coins(简单dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Pro ...
随机推荐
- MyEclipse13中修改Servlet.java源代码
Servlet.java源代码想要修改的步骤,与低版本的不同废话少说,直接来步骤: 1,在myEclipse的安装目录中搜索com.genuitec.eclipse.wizards文件,如图:选择co ...
- qt4.8.4安装以及64位程序编译方法
本文将使用简单的几个步骤说明在vc2008和64位的操作系统下如何编译安装x64Qt软件 首先必须保证你所使用的系统是64bit的操作系统,本次我们使用的系统是windows7 professiona ...
- Cookie获取、设置值
设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...
- HTML5入门(一)
HTML简单介绍: HTML(HyperText Markup Language),超文本标记语言,是一种专门用于创建web的超文本文档编程语言,是我们看到的网页的源代码. 版本简介: 1997年推出 ...
- SSIS DB目录设置 (Integration Services Catalogs)
1.创建SSISDB目录 这里没什么好说的,点击Enable CLR Integration ,然后设置一个加密密码 2. SSIS Catalog设置 Retention Period (days) ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- HDU3496-Watch The Movie
描述: New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight ...
- php基础知识(每天分享一些以前的笔记希望能帮助自学的朋友)
php基础(第一天) php标签 1. 要知道php是一种嵌入html文档的脚本语言:php语法格式是:<?php 想要写的内容 ?>红色体就是php的标签,注意这些标签都要在英式输入法 ...
- mybatis+spring+c3p0+maven+ehcache
项目截图 pom.xml如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- discuz默认模板文件结构详解-模板文件夹介绍
| — template — default 系统内置风格模板(默认风格)| — template — default – discuz_style_default.xml 风格安装文件,可用 ...