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

高手帮我看下,也是什么bt数据都通过了,但还是WA.是不是我忽略了什么还是漏掉了什么呢?

Posted by dljlw at 2007-04-24 15:58:57 on Problem 1011
/*
这是一道典型DFS+剪枝的经典题,推荐大家都去做做。
本题大概意思就是:有n数,分成若干若干组,每组合都应为lg,求lg的最小值。
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10000
int array[N],assis[N],x[N];
int result=0,mark=0;

int GD(int n)
{
	int i,k=1;
	for(i=0;i<mark;i++)
	{
		if(assis[i]==n)k=0;
	}
	return k;
}

int DFS(int num,int length,int m)
{
	int i,j,t,k=0;
	int temp=0,count=0;
	//printf("m=%d\n",m);
	if(m>=length)return 1;
	else
	for(i=0;i<length;i++)
	{
		if(!x[i])
		{
			temp=array[i];x[i]=1;if(temp==num && i==0)k=1;
			for(j=i+1;j<length;j++)
			{
				if(!x[j])
				{
					temp+=array[j];x[j]=1;
					if(temp==num)
					{
						for(t=0;t<length;t++)if(x[t])count++;
						//for(t=0;t<length;t++)printf("t=%d,x[%d]=%d ",t,t,x[t]);system("PAUSE");
						k=DFS(num,length,m+count);if(k)break;
					}
					else if(temp>num)
					{
						temp -= array[j];x[j]=0;
					}
				}
			}
		}
		if(k)break;
		if(!k && !m){for(t=0;t<length;t++)x[t]=0;count=0;}
	}
	return k;
}

void sort(int n)
{
	int i,j,temp;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(array[i]<array[j]){temp=array[i];array[i]=array[j];array[j]=temp;}
		}
	}
}

int test(int num,int n)
{
	int i,k=1,t=0;
	for(i=0;i<n;i++)
	{
		if(num==array[i])t++;
	}
	if(t==n)k=0;
	return k;
}

int main()
{
	int i,n;
	while(1)
	{
		scanf("%d",&n);
		if(n==0)break;
		if(n==1)
		{
			int t=0;
			scanf("%d",&t);
			printf("%d\n",t);
		}
		else
		{
			int temp=0;
			for(i=0;i<n;i++)
			{
				scanf("%d",&array[i]);
				result+=array[i];
			}
			sort(n);
			//for(i=0;i<n;i++)printf("%d ",array[i]);printf("\n");
			temp=array[0];

			if(!test(temp,n))printf("%d\n",temp);

			else
			{
				for(i=2;i<=result;i++)
				{
					if(result%i==0 && GD(i) && i>temp)assis[mark++]=i;
				}
				if(mark<=1)
				{
					mark=0;
					for(i=2;i<result;i++)
					{
						if(result%i==0 && GD(i))assis[mark++]=i;
					}
				}
				//for(i=0;i<mark;i++)printf("%d ",assis[i]);printf("\n");
				for(i=0;i<mark;i++)
				{
					if(DFS(assis[i],n,0)){printf("%d\n",assis[i]);break;}
					else memset(x,0,sizeof(int)*n);
				}
			}
			memset(assis,0,sizeof(int)*mark);
			memset(array,0,sizeof(int)*n);
			memset(x,0,sizeof(int)*n);
			result=mark=0;
		}
	}
	return 0;
}

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