hdu 1518 Square 深搜,,,,花样剪枝啊!!!

Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9588    Accepted Submission(s): 3127

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

開始就理解错题意了

o(╯□╰)o 为什么我总是理解错题意

题目的意思是全部的木棍能否组成一个正方形,而我觉得全部木棍中的一部分能否够构成一个正方形。。

一直都是TLE,,我是枚举了全部的正方形可能的长度,然后进行深搜。

。。

后来看了别人的代码才返现是自己理解错了。

即使题目意思明确了。我还是TLE一次。。原因是我反复搜索了。。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std ;

int d[30] , m , sum = 0;
bool visited[30] ;
bool DFS(int len , int c ,int pos)
{
	if(c==4)
	{
		return true ;
	}
	if(sum == len)
	{
		if(DFS(0,c+1,0))
		{
			return true ;
		}
	}
	else
	{
		for(int i = pos ; i < m ; ++i)
		{
			if(!visited[i])
			{
				if(len+d[i]>sum)
				{
					return false;
				}
				visited[i] = true ;
				if(DFS(len+d[i],c,i+1))
				{
					return true ;
				}
				visited[i] = false ;
			}
		}
	}
	return false ;
}

int main()
{
	int n ;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		sum = 0 ;
		for(int i = 0 ; i < m ; ++i)
		{
			scanf("%d",&d[i]) ;
			sum += d[i] ;
		}
		if(m<4 || sum%4!=0)
		{
			puts("no") ;
		}
		else
		{
			sort(d,d+m) ;
			sum /= 4 ;
			if(sum<d[m-1])
			{
				puts("no") ;
				continue ;
			}
			memset(visited,false,sizeof(visited)) ;
			if( DFS(0,0,0) )
			{
				puts("yes") ;
			}
			else
			{
				puts("no") ;
			}
		}
	}
	return 0 ;
}
时间: 2024-07-29 21:38:02

hdu 1518 Square 深搜,,,,花样剪枝啊!!!的相关文章

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

HDU 1518 Square 搜索

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vario

回溯深搜与剪枝初步

回溯算法也称试探法,一种系统的搜索问题的解的方法,是暴力搜寻法中的一种.回溯算法的基本思想是:从一条路往前走,能进则进.回溯算法解决问题的一般步骤: 根据问题定义一个解空间,它包含问题的解 利用适于搜索的方法组织解空间 利用深度优先法搜索解空间,并且在搜索过程中用剪枝函数避免无效搜索 回溯法采用试错的思想,它尝试分步的去解决一个问题.在分步解决问题的过程中,当它通过尝试发现现有的分步答案不能得到有效的正确的解答的时候,它将取消上一步甚至是上几步的计算,再通过其它的可能的分步解答再次尝试寻找问题的

深搜优化剪枝

之前做过不少深搜题,很多TLE,所以剪枝很重要,如何“未雨绸缪”,避免不必要的搜索树分支? 例题: 数的划分 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5: 1,5,1: 5,1,1: 问有多少种不同的分法. 输出一个整数,即不同的分法. 由题意得,其分发出现重复数字组合则为同一种算法,那么保证1-k个数递增即可 代码: #include<bits/stdc++.h> using namespace std;

一本通例题埃及分数—题解&amp;&amp;深搜的剪枝技巧总结

一.简述: 众所周知,深搜(深度优先搜索)的时间复杂度在不加任何优化的情况下是非常慢的,一般都是指数级别的时间复杂度,在题目严格的时间限制下难以通过.所以大多数搜索算法都需要优化.形象地看,搜索的优化过程就像将搜索树上没用的枝条剪下来,因此搜索的优化过程又叫剪枝.剪枝的实质就是通过判断决定是否要沿当前枝条走下去. 二.搜索的剪枝必需遵循三个原则: 1.正确性(不能把正解排除,要不然搜什么呢?)2.准确性(尽可能把不能通向正解的枝条剪去)3.高效性(因为在每个枝条上都要进行一次判断,如果判断的复杂