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

没特别搞什么优化 dfs297ms

Posted by 1100012760 at 2012-06-26 01:26:02 on Problem 3740
第一次理解成选一些列使每行均有且只有一个一 想着正好用状压 果断悲剧。。。
后来又写错一个循环上界 wa了数次后 猛然发现 
感觉我的代码挺短的 来和大家分享一下

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int state[20][320] , m , n , num[20];
bool flag;
void dfs(int l , int*p , int left)
{
	int tem[320] , i , j;
	if(left==0)
	{
		flag = true;
		return;
	}
	for (i = l;  i <=m ; i++)
	{
		if(num[i]==0) continue;
		if(num[i]>left) continue;
		for (j = 1 ; j <= n ; j++)
		{
			if((state[i][j]&p[j])!=0) break;
			tem[j] = (state[i][j]|p[j]);
		}
		if (j>n) dfs(i+1,tem,left-num[i]);
		if (flag) return;
	}
	return;
}	
int main()
{
	int i , j , p[320];
	while(cin>>m>>n)
	{
		flag = false;
		memset(p,0,sizeof(p));
		memset(num,0,sizeof(num));
		for (i = 1 ; i <= m ; i++)
		{
			for (j = 1 ; j <= n ; j++)
			{
				scanf("%d",&state[i][j]);
				if(state[i][j]==1) num[i]++;
			}
		}		
		dfs(1,p,n);		
		if(flag) printf("Yes, I found it\n");
		else printf("It is impossible\n");		
	}
	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