http://poj.org/problem?id=1321
摆放棋子,摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,求摆放k个棋子的所有可行的摆放方案C。
类似n皇后。
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=10;
char G[maxn][maxn];
bool use[maxn];
int n,k;
int ans,now;
void dfs(int x){
if (now==k){
ans++;
return;
}
if (x>=n){
return;
}
for (int j=0;j<n;j++){
if (!use[j]&&G[x][j]=='#'){
now++;
use[j]=true;
dfs(x+1);
now--;
use[j]=false;
}
}
dfs(x+1);
}
int main(){
while(cin >> n >> k){
if (n==-1){
return 0;
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cin >> G[i][j];
}
}
ans=now=0;
memset(use,false,sizeof(use));
dfs(0);
cout << ans << endl;
}
}