题目:http://poj.org/problem?id=1321
参考博客:http://blog.csdn.net/lyy289065406/article/details/6647674
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack> 6 #include<queue> 7 #include<iomanip> 8 #include<cmath> 9 #include<map> 10 #include<vector> 11 #include<algorithm> 12 using namespace std; 13 14 int n,k,cnt; 15 int G[10][10],vis[10]; 16 17 void dfs(int r,int num) 18 { 19 int i; 20 if(num==k) 21 { 22 cnt++; 23 return; 24 } 25 if(r>n) return; 26 for(i=1; i<=n; i++) 27 if(G[r][i]&&!vis[i]) 28 { 29 vis[i]=1; 30 dfs(r+1,num+1); 31 vis[i]=0; 32 } 33 dfs(r+1,num); 34 return; 35 } 36 int main() 37 { 38 int i,j; 39 char ch; 40 while(cin>>n>>k&&(n!=-1||k!=-1)) 41 { 42 cnt=0; 43 memset(G,0,sizeof(G)); 44 memset(vis,0,sizeof(vis)); 45 for(i=1; i<=n; i++) 46 { 47 for(j=1; j<=n; j++) 48 { 49 cin>>ch; 50 if(ch=='#') 51 G[i][j]=1; 52 } 53 getchar(); 54 } 55 dfs(1,0); 56 cout<<cnt<<endl; 57 } 58 return 0; 59 }