POJ 1321 棋盘问题
百年难得一遇的会做的中文题......
每一次DFS都有两种状态 放或者不放 简单的DFS模板题。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 7 using namespace std; 8 9 int n,k,sum; 10 11 char map[10][10]; 12 13 bool HashH[10],HashW[10]; 14 15 void dfs(int h,int w,int kc) 16 { 17 int i,j; 18 19 if(kc == k) 20 { 21 ++sum; 22 return ; 23 } 24 25 for(i = h; i <= n; ++i) 26 { 27 for(j = (i == h ? w+1 : 1); j <= n; ++j) 28 if(map[i][j] == '#' && HashH[i] == false && HashW[j] == false) 29 break; 30 if(j != n+1) 31 break; 32 } 33 34 if(map[i][j] == '#') 35 { 36 HashH[i] = true; 37 HashW[j] = true; 38 dfs(i,j,kc+1); 39 HashH[i] = false; 40 HashW[j] = false; 41 dfs(i,j,kc); 42 } 43 } 44 45 int main() 46 { 47 int i; 48 while(scanf("%d %d",&n,&k) && (n != -1 || k != -1)) 49 { 50 51 sum = 0; 52 53 memset(HashH,false,sizeof(HashH)); 54 memset(HashW,false,sizeof(HashW)); 55 56 for(i = 1; i <= n; ++i) 57 { 58 scanf("%s",map[i]+1); 59 } 60 61 dfs(1,0,0); 62 63 printf("%d\n",sum); 64 } 65 return 0; 66 }