#include "iostream"
#include "cmath"
#include "cstdlib"
#include "ctime"
using namespace std;
class Queen
{
public:
friend void nQueen(int);
private:
int *x, *y;
int n;
bool place(int k);
bool QueenLV();
};
bool Queen::place(int k)
{
for(int i=1; i<k; i++)
if(abs(x[i]-x[k])==abs(i-k) || x[i]==x[k])
return false;
return true;
}
bool Queen::QueenLV()
{
int k = 1;
int count = 1;
while(k<=n && count>0)
{
count = 0;
for(int i=1; i<=n; i++)
{
x[k] = i;
if(place(k))
y[count++] = i;
}
if(count > 0)
x[k++] = y[rand()%count];
}
return count > 0;
}
void nQueen(int n)
{
Queen queen;
queen.n = n;
queen.x = new int[n+1];
queen.y = new int[n+1];
bool success = false;
while(!success)
{
memset(queen.x, 0, sizeof(queen.x));
memset(queen.y, 0, sizeof(queen.y));
if(queen.QueenLV())
{
success = true;
for(int i=1; i<=n; i++)
cout << queen.x[i] << " ";
}
}
}
int main()
{
srand(time(NULL));
cout << "输入皇后个数:";
int n;
cin >> n;
nQueen(n);
cout << endl << endl;
return 0;
}