题意 : 在给定的棋盘上放子 求方案数
思路 :dfs 见注释
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 9;
int n, k;
int vis[maxn];
int total;
char mp[maxn][maxn];
void dfs(int row, int num){if ( num == k ){ //满足条件total++;return;}if ( row >= n && num != k ) //边界return;for ( int i = 0; i < n; i++ ){ //从左往右遍历//if ( mp[row][i] != '#' || vis[i] == 1 ) continue; //不能放if ( mp[row][i] == '#' && vis[i] == 0){ //可以放vis[i] = 1; //标记列dfs(row+1,num+1); //往下遍历vis[i] = 0; //走回来了}}dfs(row+1, num);//到下一行
}
int main(){while (cin >> n >> k){if(n == -1)break;for (int i = 0; i < n; i++ ){scanf("%s",mp[i]);}total = 0;memset(vis,0,sizeof(vis));dfs(0, 0);cout << total << endl;}return 0;
}