#include<iostream>
using namespace std;
int board[8] = {0,0,0,0,0,0,0,0};
void print_board();
int illegal(int);
void eight_queen(int);
int main()
{
eight_queen(0);
return 0;
}
void eight_queen(int i)
{
if (i >= 8)
print_board();
else
for (int j=0; j<8; j++)
{
board[i] = j;
if ( !illegal(i) )
eight_queen(i+1);
}
}
int
illegal(int n)
{
for (int i=0; i< n; i++)
for (int j=i+1; j <= n; j++)
{
if (board[i] == board[j] ||
abs(board[i]-board[j]) == abs(i-j) )//board[i] == board[j]是为了保证不在一列上
return 1;
//abs(board[i]-board[j]) == abs(i-j)保证不再对角线上
}
return 0;
}
void print_board()
{
static int cnt = 1;
printf("ANS: %d\n", cnt++);
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
if (j==board[i])
printf(" X");
else
printf(" -");
cout << endl;
}
}