queen.cpp

来自「随机算法」· C++ 代码 · 共 51 行

CPP
51
字号
// Queen.cpp: implementation of the CQueen class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Queen.h"
#include "RandomNumber.h"
#include "math.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern node;
CQueen::CQueen()
{

}

CQueen::~CQueen()
{

}
bool CQueen::Place(int k)
{
	for(int j=1;j<k;j++)
		if((abs(k-j)==abs(x[j]-x[k]))||(x[j]==x[k]))
			return false;
	return true;
}
bool CQueen::QueensLV(void)
{
	CRandomNumber rnd;
	int k=1;
	int count=1;
	node=0;
	while ((k<=n)&&(count>0))
	{
		count=0;
		int j=0;
		for(int i=1;i<=n;i++)
		{
			x[k]=i;
			node++;
			if(Place(k))
				if(rnd.Random(++count)==0)
					j=i;
		}
	if(count>0)
		x[k++]=j;
	}
	return(count>0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?