Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个剪枝条件错了吗,为什么去掉就AC了

Posted by ssq at 2006-03-15 18:44:05 on Problem 2362
//#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
const int part = 4;
bool used[30] = {false};
int len[30];
int length;
int find1 = 0;
int n;
void dfs(int level,int curlen,int pth);
void fit(int pth)
{
	if(pth > 4)
	{
		find1 = 1;
	//	cout<<"yes\n";
		return ;
	}
	if(find1) return ;
	int i;
	for( i = 0 ; i <n ; i++)
	{
		if(!used[i])
			break;
	}
	used[i] = true;
	dfs(i,len[i],pth);
	used[i] = false;
}
void dfs(int level,int curlen,int pth)
{
	
	if(curlen == length )
	{
		fit(pth+1);
		return ;
	}
	if(level+1> n )
		return ;
	int i;

	for( i = level+1; i<n; i++)
	{
		if(find1) return ;
		if(!used[i])
		{
			if(curlen+len[i]<=length)
			{
				used[i] = true;
				dfs(i,curlen+len[i],pth);
				used[i] = false;
			}
		}
	}
}
int main()
{
	int testcase;
	int i,j;
	int total; 
//	cin>>testcase;
	scanf("%d",&testcase);
	for(i =1 ;i<=testcase; i++)
	{
		total = 0 ;
		find1 = 0;
	//	cin>>n;
		scanf("%d",&n);
		for(j = 0 ; j<n; j++)
		{
		//	cin>>len[j];
			scanf("%d",&len[j]);
			total +=len[j];
		}
	    if(total % 4 != 0)
			goto output;
		length = total/4;
		memset(used,false,sizeof(used));
		sort(len,len+n,greater<int>());
		//if don't add sort it cost 64 ms ,add it 0ms
		/*for(j = 0 ; j<n; j++)
		{//这个for循环出错了吗?
		//	printf("%d ",len[j]);
			if(len[j]>length)
				goto output;
			else if(len[j] == length)
			{
				if(n != 4)
					goto output;
			}
		}*/
		//when adding the cutoff method it go wa 
		
		fit(1);
output:
		if(find1 == 1) 
		//	cout<<"yes\n";
		   printf("yes\n");
		else //cout<<"no\n";
			printf("no\n");
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator