place测试算法
源代码在线查看: nqueens.cpp
//
// n-皇后问题
// 蓝翔
// 2003.12.10
////////////////////////////////////////////////
#include "StdAfx.h"
NQUEENS::NQUEENS(int n)
{
this->m_n = n;
this->m_X = new int[n];
result = 0;
int tt[] = {1,3,5,2,4};
for(int i = 0;i < 4;i++)
{
this->m_X[i] = tt[i];
}
}
NQUEENS::~NQUEENS()
{
delete[] this->m_X;
}
void NQUEENS::handle()
{
result = this->place(5);
}
int NQUEENS::place(int k)
{
int i = 1;
while(i < k)
{
if(this->m_X[i] == this->m_X[k] || //在同一列有两个皇后
abs(this->m_X[i] - this->m_X[k]) == abs(i - k)) //在同一条斜线上
return 0;
i++;
}
return 1;
}
void NQUEENS::printResult()
{
if(result == 0)
cout else
cout }