八皇后问题

//====.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();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值