This commit is contained in:
xkm
2026-06-11 19:11:25 +08:00
parent 459af53f27
commit f49c6c7938
2 changed files with 48 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
# 八皇后
[P1219](https://www.luogu.com.cn/problem/P1219)
[code](https://www.luogu.com.cn/record/138423182)
+43
View File
@@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
int xie[26], nxie[26];
int wei[13], h[13];
int sum;
void dfs(int d, int n)
{
if (d == n)
{
sum++;
if (sum <= 3)
{
for (int i = 0; i < n; i++)
{
cout << wei[i]+1 << ' ';
}
cout << endl;
}
}
for (int i=0;i<n;i++)
{
if (!h[i] && !xie[i+d] && !nxie[n-i-1+d])
{
h[i]=xie[i+d]=nxie[n-i-1+d]=1;
wei[d]=i;
dfs(d+1,n);
h[i]=xie[i+d]=nxie[n-i-1+d]=0;
}
}
}
int main()
{
int n;
cin >> n;
dfs(0, n);
cout << sum;
return 0;
}