dfs poj 1321

// http://poj.org/problem?id=1321
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


int n,k;
char map[10][10];
int dy[10];
int cnt,count;
void dfs(int x) {
	if(cnt==k){
		count++;
		return;
	}
	if(cnt<0)
		return ; 
	if(x>=n){
		return ;
	}

	for(int i=0; i<n; i++) {   //当前行进法的可能情况
		if(map[x][i]=='#' && dy[i]==0){
			cnt++;
			dy[i]=1;
			dfs(x+1); //
			cnt--;
			dy[i]=0;	
		}
	}
	dfs(x+1);  // 跳转到下一行
}


int main() {
	freopen("p1321.txt","r",stdin);
	while(cin>>n>>k) {
		if(n==-1&&k==-1)
			break;
		memset(map,0,sizeof(map));	
		for(int i=0; i<n; i++) {
			scanf("%s",&map[i]);
		}
		count=0;
		cnt=0;
		memset(dy,0,sizeof(dy));
	
		dfs(0);
		cout<<count<<endl;
	}

	return 0;
}

猜你喜欢

转载自www.cnblogs.com/chichina/p/12659376.html
今日推荐