A - 棋盘问题 POJ - 1321
1 //纯dfs() 2 3 #include <iostream> 4 #include <algorithm> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 9 using namespace std; 10 const int maxn = 10; 11 char g[maxn][maxn]; 12 bool vis[maxn]; //列标记 13 int n, k; 14 int ans; 15 16 void dfs(int nowx,int num){ 17 if (num == k){ 18 ans++; 19 return; 20 } 21 22 if (nowx >= n) //越界 23 return; 24 25 for (int i = 0; i < n; i++){ 26 if (g[nowx][i] == '#' && !vis[i]){ 27 vis[i] = 1; 28 dfs(nowx + 1, num + 1); 29 vis[i] = 0; 30 } 31 } 32 dfs(nowx + 1, num); 33 return; 34 } 35 36 int main(){ 37 while (~scanf("%d %d", &n, &k)){ 38 if (n == -1 && k == -1) 39 break; 40 ans = 0; 41 memset(vis, 0, sizeof(vis)); 42 for (int i = 0; i < n; i++){ 43 scanf("%s", &g[i]); 44 } 45 dfs(0, 0); 46 printf("%d\n", ans); 47 } 48 //system("pause"); 49 return 0; 50 }