在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 char map[50][50]; 6 7 bool vy[50]; 8 int count; 9 int n,k; 10 11 void dfs(int x,int num){//x为行数,num为棋子数 12 if(x>n){ 13 if(num==k) count++; 14 return ; 15 16 } 17 18 for(int j=0;j<n;j++){ 19 if(vy[j]==0&&map[x][j]==‘#‘){//判断当前行是否可以放棋 20 vy[j]=1; 21 dfs(x+1,num+1); 22 vy[j]=0; 23 } 24 25 } 26 dfs(x+1,num); 27 } 28 int main(){ 29 while(scanf("%d%d",&n,&k)!=EOF){ 30 if(n==-1&&k==-1) break; 31 count=0; 32 33 memset(vy,0,sizeof(vy)); 34 for(int i=0;i<n;i++){ 35 for(int j=0;j<n;j++){ 36 cin>>map[i][j]; 37 38 } 39 } 40 dfs(0,0); 41 printf("%d\n",count); 42 } 43 return 0; 44 }
原文:https://www.cnblogs.com/jin0622/p/14346289.html