题意:给一个n×n的棋盘,#是可以放棋子的地方,棋子每行每列只能有一个,问放k个棋子的方案数。
解法:按行深搜,标记一下列就行了。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int n, m;
string maze[10];
int vis[10];
int ans = 0;
void dfs(int i, int cnt)
{
if(cnt == m)
{
ans++;
return ;
}
if(i == n)
return ;
for(int j = 0; j < n; j++)
{
if(maze[i][j] == '#' && !vis[j])
{
vis[j] = 1;
dfs(i + 1, cnt + 1);
vis[j] = 0;
}
}
dfs(i + 1, cnt);
}
int main()
{
while(~scanf("%d%d", &n, &m) && !(n == -1 && m == -1))
{
memset(vis, 0, sizeof vis);
for(int i = 0; i < n; i++)
cin >> maze[i];
ans = 0;
dfs(0, 0);
printf("%d\n", ans);
}
return 0;
}