poj1321 深搜

这题开始自己没想出来,是看了别人的思路才明白的。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <math.h>
 4 #include <string>
 5 #include <string.h>
 6 using namespace std;
 7 int res,k,n;
 8 bool gird[10][10];
 9 bool v_col[10];
10 void dfs(int row,int number)
11 {
12     if(number==k)
13     {
14 
15         res++;
16         return ;
17     }
18     if(row>n)
19         return ;
20     for(int i=0;i<n;++i)
21     {
22         if(gird[row][i]&&!v_col[i])
23         {
24             v_col[i]=1;
25             dfs(row+1,number+1);
26             v_col[i]=0;
27         }
28     }
29     dfs(row+1,number);          //这里是最关键的地方,
30                                 //意思就是这行先不放棋子了,
31                                 //状态转移到在第row+1行放number个棋子
32 
33     return ;
34 }
35 int main()
36 {
37     string temp;
38     int i,j;
39     while(cin>>n>>k)
40     {
41         if(n==-1&&k==-1)
42             break;
43         res=0;
44         memset(gird,0,sizeof(gird));
45         memset(v_col,0,sizeof(v_col));
46         for(i=0;i<n;++i)
47         {
48             cin>>temp;
49             for(j=0;j<temp.length();++j)
50             {
51                 if(temp[j]=='#')
52                     gird[i][j]=1;
53             }
54         }
55         dfs(0,0);
56         cout<<res<<endl;
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/symons1992/p/2966852.html