搜索可以解决
2025-04-05 15:31:31
发布于:江苏
2阅读
0回复
0点赞
#include <bits/stdc++.h>
#define endl "\n"
#define debug freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout)
using namespace std;
int n, a[7][7], cnt, ans[7];
void dfs(int x)
{
if (x == 1)
{
ans[x] = 1;
dfs(x + 1);
return;
}
if (x == n + 1)
{
cnt++;
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
printf("\n");
return;
}
for (int i = 1; i <= 4; i++)
{
bool flag = true;
for (int j = 1; j <= n; j++)
{
if (a[x][j] && i == ans[j])
{
flag = false;
break;
}
}
if (flag)
{
ans[x] = i;
dfs(x + 1);
ans[x] = 0;
}
}
}
int main()
{
// freopen("in.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
dfs(1); // 递归搜索
cout << cnt << endl;
return 0;
}
这里空空如也
有帮助,赞一个