Acwing 1114. 棋盘问题
作者:互联网
题目链接:
分析:
与N皇后问题相类似。
需要注意当前行搜完,需要从下一行继续搜索,不能从头开始搜。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 15;
int n, k, ans;
char road[N][N];
bool y[N];
void dfs(int u, int cnt)
{
if (cnt == k)
{
ans ++ ;
return;
}
for (int j = u; j < n; j ++ )
{
for (int i = 0; i < n; i ++ )
{
if (road[j][i] == '#' && !y[i])
{
y[i] = true;
dfs(j + 1, cnt + 1);
y[i] = false;
}
}
}
}
int main()
{
while (~scanf("%d%d", &n, &k) && ~n && ~k)
{
ans = 0;
memset(road, 0, sizeof(road));
memset(y, 0, sizeof(y));
for (int i = 0; i < n; i ++ ) scanf("%s", road[i]);
dfs(0,0);
printf("%d\n", ans);
}
return 0;
}
标签:cnt,int,dfs,++,1114,ans,棋盘,road,Acwing 来源: https://blog.csdn.net/weixin_60484917/article/details/122389993