poj 1321 棋盘问题

http://poj.org/problem?id=1321  dfs

View Code
 1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 char mat[10][10];
5 bool col[10];//col[i]=true表示第 i列有放
6 int n,k,solution=0;
7 int num;//当前放了几个
8 void initMat()
9 {
10 memset(col,false,sizeof(col));
11 solution=0;
12 num=0;
13 }
14 bool ok(int i,int j)//判断i,j处能不能放
15 {
16 if(col[j]==false && mat[i][j]=='#') return true;
17 return false;
18 }
19 void dfs(int cur)
20 {//cur为某一行
21 if(num==k)
22 {
23 solution++;
24 return;
25 }
26 if(cur==n) return;
27 for(int j=0;j<n;j++)//cur是行,j是列
28 {
29 if(ok(cur,j))
30 {
31 num++;
32 col[j]=true;
33 dfs(cur+1);
34 num--;
35 col[j]=false;
36 }
37 }
38 dfs(cur+1);
39 }
40 int main()
41 {
42 while(cin>>n>>k)
43 {
44 if(n==-1 && k==-1) break;
45 initMat();
46 int i,j;
47 for(i=0;i<n;i++)
48 for(j=0;j<n;j++) cin>>mat[i][j];
49 dfs(0);
50 cout<<solution<<endl;
51 }
52 return 0;
53 }


 

posted @ 2012-04-05 22:09  keepmoving89  阅读(112)  评论(0编辑  收藏  举报