POJ 1321 棋盘问题

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int n,k;
 6 char m[9][9];
 7 int a[9];
 8 
 9 void dfs(int cur,int k,int& tot)
10 {
11     if(k<0)  return;
12     if(cur==n)
13     {
14         if(k==0)
15             tot++;
16         return ;
17     }
18     for(int i=0;i<n;i++)
19     {
20         if(m[cur][i]=='#'&&a[i]==0)
21         {
22             a[i]=1;
23             dfs(cur+1,k-1,tot);
24             a[i]=0;
25         }
26     }
27     dfs(cur+1,k,tot);
28 }
29 
30 int main()
31 {
32     while(cin>>n>>k&&n!=-1&&k!=-1)
33     {
34 
35     for(int i=0;i<n;i++)
36     {
37         for(int j=0;j<n;j++)
38             cin>>m[i][j];
39     }
40     for(int i=0;i<n;i++)
41         a[i]=0;
42 
43     int tot=0;
44     dfs(0,k,tot);
45     cout<<tot<<endl;
46     }
47 
48     return 0;
49 }

 

posted @ 2013-04-11 11:54  码代码的猿猿  阅读(126)  评论(0编辑  收藏  举报