杭电 1518 Square (深搜)(回溯)

http://acm.hdu.edu.cn/showproblem.php?pid=1518

Square

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

Total Submission(s): 8343    Accepted Submission(s): 2706

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

AC代码:

#include<iostream>
#include<cstring>
using namespace std;  

int v[23],n,flag;
int a[23],sum;  

void dfs(int p,int ans,int t)
{
	if(ans==sum/4)
	{
		p++;
	//	cout<<p<<"-------\n";
		if(p==4)
		{
			flag=1;
			return ;
		}
		else ans=0;
		t=0;
	}  

	if(flag)   return ;
	for(int i=t;i<n;i++)
		if(!v[i] && a[i]+ans<=sum/4)
		{
			v[i]=1;
		//	cout<<ans+a[i]<<"   ";
			dfs(p,ans+a[i],i);
			v[i]=0;
		}
}  

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n;
		int max;
		max=sum=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			sum += a[i];
			if(max<a[i])   max=a[i];
		}  

		if(sum%4!=0 || max>sum/4)
			cout<<"no\n";
		else
		{
			memset(v,0,sizeof(v));
			flag=0;
			dfs(0,0,0);
			if(flag)   cout<<"yes\n";
			else     cout<<"no\n";
		}
	}  

	return 0;
}

杭电 1518 Square (深搜)(回溯),布布扣,bubuko.com

时间: 2024-10-14 19:28:40

杭电 1518 Square (深搜)(回溯)的相关文章

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

HDU5723 Abandoned country 最小生成树+深搜回溯法

Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guarante

POJ 1321 棋盘问题 深搜+回溯

棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24958 Accepted: 12333 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一

杭电1518

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

杭电ACM1398——Square Coins~~母函数

这一题,简单的母函数的应用,好久没有写过母函数了,有点生疏了. 题目的硬币有17种,分别是1到17的平方的硬币. 下面的是AC的代码: #include <iostream> #include <cstring> #include <cmath> using namespace std; int dp[305], temp[305]; int main() { int i, j, k, l; for(i = 0; i < 305; i++) //初始化 { dp[

A计划 HDU杭电2102【广搜+STL队列】

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位

hdu1518(Square)深搜+剪枝

点击打开杭电1518 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,

HDU 1045 Fire Net【DFS深搜】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

hdu 1518 Square(深搜dfs)

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