_hello_word_ @ 2024-07-10 19:09:18
#include <bits/stdc++.h>
using namespace std;
int n, cnt = 0, a[15][15] = { 0 }, ans[15];
void seta(int i, int j, int p)
{
ans[i] = j;
a[i][j] = 1;
for (int k = 1; k <= n; k++) // 同行同列
{
a[k][j] = p;
a[i][k] = p;
}
for (int m = 1; m <= n; m++)
{ // 副对角线方向
for (int l = 1; l <= n; l++)
{
if (m + l == i + j)
a[m][l] = p;
if (m - l == i - j) // 主对角线方向
a[m][l] = p;
}
}
}
void dfs(int pos)
{
if (pos == n + 1)
{
cnt += 1;
if (cnt <= 3) {
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
cout << endl;
}
}
else if (pos < n + 1)
{
for (int j = 1; j <= n; j++)
{
if (!a[pos][j])
{
int b[15][15] = { 0 };
memcpy(b, a, sizeof(a));
seta(pos, j, 1);
dfs(pos + 1);
memcpy(a, b, sizeof(a));
}
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << cnt;
return 0;
}
by NEKO_Daze @ 2024-07-10 19:13:08
@_helloword 你这 dfs 是不是没回溯啊?
by _hello_word_ @ 2024-07-11 08:32:18
@NEKO_Daze memcpy(a,b,sizeof(a))算不算回溯啊?虽然占空间有点大
by hzy99999 @ 2024-07-11 09:13:50
1、一次memcpy的时间复杂度是O(n^2),这样肯定超时,不需要b数组,你直接对a数组进行操作以及回溯就行了
2、你给对角线做标记的操作是O(n^2),这样也会超时
3、打标记的时候不能只有0和1,应该标记的时候+1,回溯的时候-1,这样可以表示这个格子被多少个皇后覆盖
4、你是谁呀,我为啥关注你? @_helloword
附个代码:
#include <bits/stdc++.h>
using namespace std;
int n, cnt = 0, a[15][15] = {0}, ans[15];
void seta(int i, int j, int p)
{
ans[i] = j;
a[i][j] += p;
for (int k = 1; k <= n; k++) // 同行同列
{
a[k][j] += p;
a[i][k] += p;
}
// for (int m = 1; m <= n; m++)
// { // 副对角线方向
// for (int l = 1; l <= n; l++)
// {
// if (m + l == i + j)
// a[m][l] += p;
// if (m - l == i - j) // 主对角线方向
// a[m][l] += p;
// }
// }
int x = i + 1, y = j + 1;
while (x <= n && y <= n)
a[x][y] += p, x++, y++;
x = i + 1, y = j - 1;
while (x <= n && y)
a[x][y] += p, x++, y--;
}
void dfs(int pos)
{
if (pos == n + 1)
{
cnt += 1;
if (cnt <= 3)
{
for (int i = 1; i <= n; i++)
cout << ans[i] << " ";
cout << endl;
}
}
else if (pos < n + 1)
{
for (int j = 1; j <= n; j++)
{
if (!a[pos][j])
{
seta(pos, j, 1);
dfs(pos + 1);
seta(pos, j, -1);
}
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << cnt;
return 0;
}
by GEZHENHAO @ 2024-07-12 16:41:10
@_helloword 你把数组开大一点```
using namespace std; int n, cnt = 0, a[30][30] = { 0 }, ans[15];//这里