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

请问王晓东《算法设计与分析》里的无和集问题用搜索怎么剪枝,n=4的时候就死了,附上代码

Posted by Acaini at 2009-11-03 20:43:26 and last updated at 2009-11-03 20:43:50
#include <iostream>
//#include <fstream>
using namespace std;
#define K 10000
#define N 10000
int t[N];
int sum[K][N];
bool s[K][N];
int n;
int k;
bool wuheji(int dep)
{
	if(dep > k) return true ;
	for(int i=1;i<= n ; i++)
	{
		if(sum[i][dep]== 0)
		{
   			t[dep]= i;
			s[i][dep]=true ;

			for(int j=1; j< dep; j++)
				if(s[i][j])
					sum[i][dep+j] ++ ;

			if( wuheji(dep+1) )
				return true;

			for(int j=1; j<dep; j++)
				if(s[i][j])
					sum[i][dep+j] -- ;

			s[i][dep]=false;
			t[dep]= 0 ;
		}
	}
	return false ;
}
int main()
{
	//ifstream cin(".in");
	//ofstream cout(".out");
	cin>>n;
	k=n;
	while(wuheji(1))
	{
		memset(sum,0,sizeof(sum));
		memset(s,0,sizeof(s));
		memset(t,0,sizeof(t));
		k++;
	}
	cout<<k-1<<endl;
	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