#include <iostream>
using namespace std;
int vol[8];//存放可行情况下 每行的列号 列从0到7
int number;//存放可行的数目
void Queen(int,int,bool (*)[8]);
int main()
{
bool empty[8][8]={0};
Queen(0,7,empty);
cout<<number;
system("pause");
return 0;
}
void Queen(int line, int N, bool empty[][8]) // n乘n的棋盘 从 line行开始 empty(i,j) =0表示第i行第j列可以放皇后 i从0到7 j从0到7
{
if (line>7)
{
return;
}
int i = line,j,m,n;
bool hasposition =false;
for(j = 0;j < 8;j++)
{
if (empty[i][j] == 0)
{
bool a[8][8];
for(int k =0;k<8;k++)
for(int p = 0;p<8;p++)
a[k][p] = empty[k][p];
vol[line] = j;
hasposition = true;
a[i][j] = 1;
for(m = i,n =j; m<8 && n<8; m++,n++)
{
a[m][n] = 1;
}
for(m = i,n =j; m<8 && n>-1; m++,n--)
{
a[m][n] = 1;
}
for(m = i+1; m<8; m++)
{
a[m][j] = 1;
}
Queen(line + 1,N,a);
}
if( (line == N) && hasposition)
{
for(n = 0;n<8 ;n++)
cout<<vol[n];
cout<<endl;
number++;
return;
}
}
}
05-24
12-02
10-24