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
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

DFS AC代码

Posted by 452181625 at 2014-05-14 21:41:35 on Problem 1321
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char chess[10][10];
int col[10];
int n,m,ans;
void dfs(int row,int num)
{
	int i,j;
	if(num==m)
	{
		ans++;
		return;
	}
	for(i=row+1;i<=n;i++)
		for(j=1;j<=n;j++)
			if(chess[i][j]!='.' && !col[j])
			{
				col[j]=1;
				dfs(i,num+1);
				col[j]=0;
			}
			
}
int main()
{
	int i;
	while(cin>>n>>m)
	{
		ans=0;
		if(n==-1 && m==-1)break;
		memset(col,0,sizeof(col));
		for(i=1;i<=n;i++)
			cin>>chess[i]+1;
		dfs(0,0);
		cout<<ans<<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