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求32ms优化到16ms方法(附代码)

Posted by bhwl14130 at 2015-02-03 16:03:04 on Problem 1321
#include<stdio.h>
#include<string.h>
int n,sum,k,x[9],y[9];
char a[9][9];
void dfs(int f,int t)
{
	if(t==k)
	{
		++sum;
		return;
	}
	for(int i=f;i<n;++i)
	for(int j=0;j<n;++j)
	{
		if(y[j]==1)continue;
		if(a[i][j]=='.')continue;
		y[j]=1;
		dfs(i+1,t+1);
		y[j]=0;
	
	}
}
int main()
{
	char s[9];
	while(~scanf("%d%d",&n,&k)&&(n!=-1||k!=-1))
	{
		sum=0;
		memset(y,0,sizeof(y));
		for(int i=0;i<n;++i)
		{
			scanf("%s",s);
			for(int j=0;j<n;++j)
			a[i][j]=s[j];
		}
		dfs(0,0);
		printf("%d\n",sum);
	}
}

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