A

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
当为-1 -1时表示输入结束。 
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1
/*
* @Author: lyuc
* @Date:   2017-05-02 16:51:45
* @Last Modified by:   lyuc
* @Last Modified time: 2017-05-02 17:07:23
*/

#include <iostream>
#include <stdio.h>
#include <string.h>
#define LL long long
using namespace std;
int n,k;
char mapn[10][10];
int col[10];
LL res=0;
void dfs(int u,int co){//遍历到第几行
    if(co==k){
        res++;
        return ;
    }
    if(u>=n) return ;
    for(int i=0;i<n;i++){
        if(col[i]) continue;
        if(mapn[u][i]=='#'){
            col[i]=1;
            dfs(u+1,co+1);
            col[i]=0;    
        }
    }
    dfs(u+1,co);
}
void init(){
    memset(col,0,sizeof col);
    res=0;
}
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&k)!=EOF&&(n!=-1&&k!=-1)){
        init();
        for(int i=0;i<n;i++)
            scanf("%s",mapn[i]);
        dfs(0,0);
        printf("%lld
",res);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6797066.html