poj 1321

dfs


#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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章