八皇后全部解及指定皇后位置求解

编译器 VS2010

编写者 冬虫

2012.2.29

这里借助STL的向量,通过封装类Board(棋盘)实现实现了八皇后的求解问题。

1、提供了两种方式求解(1)求出八皇后的所有解(int FindAllSolution(); 共92种);(2)指定第一个皇后所在位置求解( bool Queens(int row, int col); );

2、使用接口函数void Draw();在屏幕上以棋盘形式输出每次求解结果;

3、若使用同一个Board对象多次求解,每次需调用接口函数void clearsolution();清空解空间;

核心代码如下

//eightqueen.h
class Board {
public:
	Board();
	~Board();

	void clearsolution();
	int FindAllSolution();//search for all the solutions for the eight queen problem
	bool Queens(int row, int col);//find all the solutions with the position of the first queen specified
	void Draw();//output the solutions	
	
private:
	void clear();//clear the chessboard
	void Load(const std::vector<int> &queen);//load the solution to the chessboard
	bool Queenpos(vector<int> &queen, int col, int cur_col);
	bool ifsafe(int row, int col, const int pre_col, const vector<int> &queen);

	vector < vector<int> > m_queen;//store all the solutions
	char m_item[8][8];//display the chessboard
};
//eightqueen.cpp
#include "eightqueen.h"
Board::Board(){
	memset(m_item, '-', 64*sizeof(char) );
}
Board::~Board(){
}
void Board::clearsolution(){
	m_queen.clear();
}
int Board::FindAllSolution() {
	int i=0;
	while(i<8) Queens(0, i++);
	Draw();	
	return m_queen.size();
}
bool Board::Queens(int row, int col) {
	vector<int> queen(8);
	queen[col]=row;
	Queenpos(queen, 0, col);
	if(!m_queen.empty()) return true;
	return false;
}
void Board::Draw() {
	int i, j, k=0;
	vector<int> queen;
	while( k< (int) m_queen.size() )	{
		queen=m_queen[k];
		clear();
		Load(queen);
		cout<<"solution "<<k+1<<endl;
		cout<<"   0  1  2  3  4  5  6  7"<<endl;
		for(i=0; i<8; ++i){
			cout<<i<<"  ";
			for(j=0; j<8; ++j) cout<<m_item[i][j]<<ends<<ends;
			cout<<endl;
		}

		++k;
	}
}

void Board::clear(){
	memset(m_item, '-', 64*sizeof(char) );
}
void Board::Load(const vector<int> &queen){
	int row;	
	for(int i=0; i<8; ++i) {
		row=queen[i];
		m_item[row][i]='Q';
	}
}
bool Board::ifsafe(int row, int col, const int pre_col, const vector<int> &queen) {
	for(int i=0; i<col; ++i) {
		if(queen[i]==row) return false;//same row, no possibility in same column
		else if(i+queen[i]==col+row || i-queen[i]==col-row) return false;//same diagonal
		else continue;
	}
	if(col<pre_col){
		if(queen[pre_col]==row) return false;//same row, no possibility in same column
		else if(pre_col+queen[pre_col]==col+row 
			   || pre_col-queen[pre_col]==col-row) return false;//same diagonal
		else {};
	}
	return true;
}
bool Board::Queenpos(vector<int> &queen, int col, const int pre_col){
	if(col==pre_col) ++col;
	bool solved=false;
	if(col==8) solved=true;
	else{
		int row=0;
		while(row<8) {
			if( ifsafe(row, col, pre_col, queen) ) {
				queen[col]=row;
				solved=Queenpos(queen, col+1, pre_col);
				if( !solved ) ++row;
				else {
					solved=false;
					m_queen.push_back(queen);
					++row;
				}
			}
			else ++row;
		}	
	}	
	return solved;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值