[POJ1321]棋盘问题

棋盘问题
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 65752   Accepted: 31390

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

Source

 1 #include<iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 using namespace std;
 5 int n,m,cnt;
 6 bool column[10];
 7 char f[10][10];
 8 void dfs(int step,int t)
 9 {
10     if(t>m)
11     {
12         ++cnt;
13         return;
14     }
15     if(step>n)return;
16     for(int i=1;i<=n;++i)
17         if(!column[i] && f[step][i]=='#')
18         {
19             column[i]=1;
20             dfs(step+1,t+1);
21             column[i]=0;
22         }
23     dfs(step+1,t);
24 }
25 int main()
26 {
27 //    freopen("board.in","r",stdin);
28 //    freopen("board.out","w",stdout);
29     while(~scanf("%d%d",&n,&m))
30     {
31         if(n==-1 && m==-1) break;
32         if(m>n)
33         {
34             printf("0");
35             continue;
36         }
37         for(int i=1;i<=n;++i)
38             for(int j=1;j<=n;++j)
39                 cin>>f[i][j];
40         cnt=0;
41         memset(column,0,sizeof(column));
42         dfs(1,1);
43         printf("%d\n",cnt);
44     }
45 }
46 /*
47 2 1
48 #.
49 .#
50 4 4
51 ...#
52 ..#.
53 .#..
54 #...
55 -1 -1
56 */
 

猜你喜欢

转载自www.cnblogs.com/__Kgds/p/9454383.html