//====.h
#ifndef EightQueens__
#define EightQueens__
#define NOTQUEEN -99
class EQ_
{
int Ctl; //----行L计数器
int Counter_; //----解计数器
int Lix; //----行索引
int Line[8];
int IsDanger();
void GetEQC(); //====求解八皇后棋盘EQC
void CoutEQ(); //----输出
public:
EQ_();
};
#endif // !EightQueens__
//====.cpp
#include"EightQueen.h"
#include<iostream>
#include<conio.h>
using namespace std;
#define NOTQUEEN -99
void EQ_::CoutEQ(){
cout<<++Counter_<<':';
if (Counter_ == 42)
cout<<"a";
for (int CtL = 0; CtL != 8; CtL++){
cout << endl;
for (int Lix = 0; Lix != 8; Lix++)
if (Line[CtL] == Lix)
cout << " +";
else
cout << " _";
}
cout << endl << endl;
}
EQ_::EQ_(){
for (int i = 0; i != 8; i++)
Line[i] = NOTQUEEN;
Ctl = Lix = Counter_ = 0;
GetEQC();
}
int EQ_::IsDanger(){
for (int i = 0; i != 8; i++){
if (
Ctl + Lix == i + Line[i]
||
Ctl - Lix == i - Line[i]
||
Lix == Line[i]
)
return 1;
}
return 0;
}
void EQ_::GetEQC(){
for (; Ctl <= 8 && Lix <= 8;)
{
if (IsDanger())
{
Lix++;
while (Lix == 8)
{
Line[Ctl] = NOTQUEEN;
Lix = Line[--Ctl]+1;
}
}
else
{
Line[Ctl++] = Lix;
Lix = 0;
if (Ctl == 8)
{
CoutEQ();
Lix = Line[--Ctl] + 1;
if (Lix == 8)
{
Line[Ctl] = NOTQUEEN;
Lix = Line[--Ctl] + 1;
}
}
}
}
}
//====main.cpp
#include<conio.h>
#include"EightQueen.h"
int main(){
EQ_ A;
return _getch();
}