#include <iostream>
#include <math.h>
#include <list>
using namespace std;
const int count = 8;
char pos[count] = {0};
int kinds = 0;
list<char *> l;
bool check_pos(int index) //1到count
{
for(int i = 0; i < index-1; i++) //
{
if(pos[i] == pos[index-1]) //在一列
{
return false;
}
else if(abs(pos[i] - pos[index-1]) == abs(index - i-1))//正斜线
{
return false;
}
}
return true;
}
void queen(int index) //index从1开始 第几个皇后 ,当前的坐标
{
if(index > count)
{
char*a = new char[count];
memcpy(a,pos,count);
l.push_back(a);
kinds++;
return;
}
for(int i = 1; i <= count; i++) //
{
pos[index-1] = i; //循环设置自己坐标
if(index == 1)
{
queen(index+1);
}
else
{
bool flag = false;
flag = check_pos(index); //与已经有的皇后坐标判断
if(flag) //找到皇后位置,再找下一个皇后
{
queen(index+1);
}
}
}
}
int main()
{
cout << "Hello World!" << endl;
queen(1);
cout << "find " << l.size() << endl;
list<char*>::iterator it = l.begin();
int n = 0;
while(it != l.end())
{
cout << "method " << ++n << " :" << endl;
char *a = *it;
for(int m = 0; m < count; m++)
{
// cout << (int)a[m] << " ";
cout << a[m] << " ";
}
cout << endl;
for(int i = 0; i < count; i++)
{
for(int j = 1; j <= count; j++)
{
if(a[i] == j)
{
cout << " Q";
}
else
{
cout << " *";
}
}
cout << endl;
}
it++;
delete []a;
}
return 0;
}