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

谁帮俺看看,给点好数据,回溯

Posted by dynamic_study at 2009-10-31 13:09:48 on Problem 3740
//3740
#include <iostream>
using namespace std;
#define MAXN 20
#define MAXM 310
int n,m;
bool  map[MAXN][MAXM],flag[MAXM],used[MAXM],loop;
bool ok()//是否搜索成功
{
	for (int i=0;i<m;i++)
		if (!flag[i])
			return false;
	return true;
}
bool check(int p)//判断是否满足每列1个1
{
	int i;
	for (i=0;i<m;i++)
		if (map[p][i]&&flag[i])
			return false;
	return true;
}
void backTrack(int p)
{
	if (ok())
	{
		loop=true;
		return;
	}
	if (p==n||loop)
		return;
	for (int i=p;i<n&&!loop;i++)
	{
		if (check(i))
		{
			memcpy(used,flag,m);
			for (int j=0;j<m;j++)
				flag[j]=flag[j]|map[i][j];		
			backTrack(i+1);
			memcpy(flag,used,m);
		}

	}

}
int main()
{
	int i,j;
	while (cin>>n>>m)
	{
		memset(flag,false,sizeof(flag));
		memset(used,false,sizeof(used));
		for (i=0;i<n;i++)
			for (j=0;j<m;j++)
				cin>>map[i][j];
		loop=false;
		backTrack(0);
		if (loop)
			cout<<"Yes, I found it"<<endl;
		else
			cout<<"It is impossible"<<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