#include <iostream>
#include<algorithm>
using namespace std;
int x[1000];
int n;
int ans = 0;
bool place(int s)
{
for (int i = 1; i <= s - 1; i++)
{
if (x[i] == x[s] || abs(s - i) == abs(x[i] - x[s]))
return false;
}
return true;
}
void dfs(int i)
{
if (i >n)
{
ans++;
return;
}
else {
for (int j = 1; j <= i - 1; j++)
{
x[i] = j;
if (place(j)) dfs(i + 1);
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
x[1] = i;
dfs(2);
}
cout << ans;
}
n 皇后 问题 c++ 蓝桥杯
最新推荐文章于 2024-08-03 11:47:07 发布