POJ 1321 棋盘问题

棋盘问题
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28733   Accepted: 14220

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放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        :pk29
Created Time  :2015/8/19 18:14:35
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
char mp[20][20];
int vc[10];
int n,k,num=0;
void dfs(int row,int cnt){
    if(cnt==k){
        num++;return ;
    }
    if(row>n)return ;
    for(int i=1;i<=n;i++){
        if(!vc[i]&&mp[row][i]=='#'){
            vc[i]=1;
            dfs(row+1,cnt+1);
            vc[i]=0;
        }
    }
    dfs(row+1,cnt);
    return ;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    while(cin>>n>>k){
        if(n==-1&&k==-1)break;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                cin>>mp[i][j];
            }
        num=0;
        cle(vc);
        dfs(1,0);
        printf("%d
",num);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/pk28/p/4743073.html