poj1321
简单题
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 10
bool row[maxn], col[maxn];
bool map[maxn][maxn];
int ans, n, m;
void input()
{
char ch;
memset(map, 0, sizeof(map));
getchar();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
ch = getchar();
if (ch == '#')
map[i][j] = true;
else
map[i][j] = false;
}
getchar();
}
}
void dfs(int pos, int a)
{
if (a == m)
{
ans++;
return;
}
while (pos < n * n)
{
int x = pos / n;
int y = pos % n;
if (map[x][y] && !row[x] && !col[y])
{
row[x] = col[y] = true;
dfs(pos + 1, a + 1);
row[x] = col[y] = false;
}
pos++;
}
}
int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d%d", &n, &m), !(n == -1 && m == -1))
{
input();
memset(row, 0, sizeof(row));
memset(col, 0, sizeof(col));
ans = 0;
dfs(0, 0);
printf("%d\n", ans);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步