算是水题吧。。简单的dfs搜索:
题目:给出不规则的棋盘,#表示棋盘区域,然后在上面摆放棋子,问你有几种放法
#include <iostream>
#include <string.h>
using namespace std;
const int maxn = 8;
char MAP[maxn][maxn];
int n, k, cnt;
bool vis[maxn];
void dfs(int x, int cur)
{
int i, j;
if(cur > k) return;
if(cur == k) cnt++;
else
{
for(i = x; i < n; i++)
for(j = 0; j < n; j++)
if(MAP[i][j] == '#' && vis[j] == 0)
{
vis[j] = 1;
dfs(i+1, cur+1);
vis[j] = 0;
}
}
}
int main()
{
int i, j;
while(cin>>n>>k)
{
if(n == -1 && k == -1) return 0;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
cin>>MAP[i][j];
memset(vis, 0, sizeof(vis));
cnt = 0;
dfs(0, 0);
cout << cnt << endl;
}
return 0;
}