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

请问这两个dfs代码有何区别?为何一个AC一个TLE?个人感觉刚种写法等价啊

Posted by zyf298515649 at 2010-02-16 20:37:18 on Problem 1011
bool dfs(int num,int start,int leftstick,int lefttotal) //num 小棍数目, start 当前起点 ,leftstick 当前小棍剩余长度,lefttotal 剩余总长度
{
	int i,pre=-1;
	if (leftstick==0)
	{
		leftstick=len;
		num--;
		start=0;
	}
	if (num==0)
	{
		return true;
	}
	if (leftstick>lefttotal)
	{
		return false;
	}
	for (i=start;i<n;++i)
	{
		if ((st[i])&&(pre!=stick[i])&&(leftstick>=stick[i]))
		{
			pre=st[i];
			st[i]=false;
			if (dfs(num,i+1,leftstick-stick[i],lefttotal-stick[i]))
			{
				return true;
			}
			else
			{
				st[i]=true;
			  if ((leftstick-stick[i])==0)
			  {					
					return false;
			  }
			}
		}
	}
	return false;
}

bool dfs(int num,int start,int leftstick,int lefttotal) 
{
	int i,pre=-1;
	if (leftstick==0)
	{
		leftstick=len;
		num--;
	}
	if (num==0)
	{
		return true;
	}
	if (leftstick>lefttotal)
	{
		return false;
	}
	if (leftstick==len)
	{
		start=0;
		while (!st[start])
		{
			start++;
		}
		st[start]=false;
		if (dfs(num,start+1,leftstick-stick[start],lefttotal-stick[start]))
		{
			return true;
		}
		num++;
		leftstick=0;
		st[start]=true;
		return false;
	}
}

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