POJ 1321 棋盘问题

棋盘问题

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1321
64-bit integer IO format: %lld      Java class name: Main
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放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

Source

Author

蔡错@pku
 
解题:好久没做题,先让我水几天。。。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 #include <stack>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <ctime>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 #define pii pair<int,int>
15 
16 using namespace std;
17 const int maxn = 10;
18 char table[maxn][maxn];
19 int n,k,sum;
20 bool vis[maxn];
21 void dfs(int cur,int cnt){
22     if(cnt == k){
23         ++sum;
24         return;
25     }
26     if(cur >= n || n-cur < k-cnt) return;
27     for(int i = 0; i < n; ++i){
28         if(vis[i]||table[cur][i] == '.') continue;
29         vis[i] = true;
30         dfs(cur+1,cnt+1);
31         vis[i] = false;
32     }
33     dfs(cur+1,cnt);
34 }
35 int main(){
36     while(scanf("%d %d",&n,&k),~n || ~k){
37         memset(table,'',sizeof(table));
38         memset(vis,false,sizeof(vis));
39         for(int i = sum = 0; i < n; ++i)
40             scanf("%s",table[i]);
41         dfs(0,0);
42         printf("%d
",sum);
43     }
44     return 0;
45 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4236092.html