大学时C++程序设计课程的作业题目。呵呵!
N皇后问题(含八皇后问题的扩展,规则同八皇后):在N*N的棋盘上,放置N个皇后,要求每一横行每一列,每一对角线上均只能放置一个皇后,求解可能的方案及方案数。
下面程序利用堆栈数据结构,使用回溯法求出所有可行解。
/*
Copyright (c) 2006, Computing Center of IHEP, Beijing, China
Aigui.LIU@ihep.ac.cn
链式结构堆栈类的类模板实现及用堆栈类求解N皇后问题
编写:089906-36@NUAA 刘爱贵
*/
#include "iostream.h"
#include "math.h"
#define TRUE 1
#define FALSE 0
#define ERROR -1
typedef int Status;
//用模板实现的链式结构堆栈类
template <class T>
class stack{
private:
struct link{
T data; //结点数据域
link *next; //下一结点指针
link(T Data,link* Next){//结构体构造函数
data=Data;
next=Next;
}
}*head; //堆栈顶指针
public:
stack(); //构造函数(初始化栈)
~stack(); //析构函数(销毁栈)
void push(T Data); //压栈操作
T gettop()const; //取栈顶元素
T pop(); //出栈操作
T getvalue(int index); //返回栈底开始第INDEX个栈中值
void traverse(int n); //遍历栈 N个数换行
int empty(); //判断栈是否为空,1是,0非
int sizeofstack(); //返回栈的大小
void clear(); //清空栈
};
//N皇后类
class queen{
private:
stack<int>* qStack; // 求解中所用数据结构:堆栈
public:
queen(); //构造函数
~queen(); //析构函数
Status Place(int k); // 判断当前行K列是否可以可以放置皇后
Status Queen(int n); //求出所有解
};
//类模板成员函数的实现
template<class T> stack<T>::stack()//构造函数
{
head=0;
}
template<class T> stack<T>::~stack()//析构函数
{
link* cursor=head;
while(head)
{
cursor=cursor->next;
delete head;
head=cursor;
}
}
template<class T>void stack<T>::push(T Data)//压栈操作
{
head=new link(Data,head);
}
template<class T>T stack<T>::gettop()const//取栈顶元素
{
return head->data;
}
template<class T>T stack<T>::pop()//出栈操作
{
if(head==0)return 0;
T result=head->data;
link* oldhead=head;
head=head->next;
delete oldhead;
return result;
}
template <class T>T stack<T>::getvalue(int index)//返回栈底开始第INDEX个栈中值
{
link *cursor=head;
int i=1;
int stacklen=sizeofstack();
if(index<=0||index>stacklen)return 0;
while(i<=(stacklen-index))
{
cursor=cursor->next;
i++;
}
return cursor->data;
}
template <class T> void stack<T>::traverse(int n)//遍历栈
{
link * cursor=head;
int iEnterSign=1;//换行标识
while(cursor)
{
cout<<cursor->data<<" ";
if(iEnterSign%n==0)cout<<endl;
cursor=cursor->next;
iEnterSign++;
}
if((iEnterSign-1)%n!=0)cout<<endl;
}
template <class T>int stack<T>::empty() //判断栈是否为空,1是,0非
{
return head==0?1:0;
}
template <class T>int stack<T>::sizeofstack() //返回栈的大小
{
int size=0;
link *cursor=head;
while(cursor)
{
cursor=cursor->next;
size++;
}
return size;
}
template<class T> void stack<T>:: clear() //清空栈
{
link *cursor=head;
while(cursor&&cursor->next)
{
cursor=cursor->next;
delete head;
head=cursor;
}
}
//N皇后类成员函数的实现
queen::queen() //构造函数
{
qStack=new(stack<int>);
}
queen:: ~queen() //析构函数
{
delete qStack;
}
Status queen::Place(int k) // 判断当前行K列是否可以可以放置皇后
{
int i=1,j,e;
j=qStack->sizeofstack()+1;
while(i<j)
{
e=qStack->getvalue(i);
if((k==e)||(abs(i-j)==abs(e-k))) return FALSE;
i++;
}
return TRUE;
}
Status queen::Queen(int n) //回溯法求出N皇后所有解
{
int k,m=0,e;
if(n<4)return ERROR;
k=1;
while(!qStack->empty()||k<=n)
{
while((k<=n)&&(!Place(k)))k++;
if(k<=n)
{
qStack->push(k);
if(qStack->sizeofstack()==n)
{
m++;
qStack->traverse(n);
e=qStack->pop();
k=e;
k++;
}
else k=1;
}
else
{
e=qStack->pop();
k=e;
k++;
}
}
cout<<"There are "<<m<<" ways"<<endl;
return TRUE;
}
void main(void)//程序入口函数
{
//堆栈操作示例
cout<<"堆栈操作示例:"<<endl;
stack<int> *sample=new(stack<int>);
sample->empty()==1?cout<<"Stack is empty!"<<endl:cout<<"Stack is not empty!"<<endl;
int i;
for(i=1;i<=10;++i)sample->push(i);
sample->traverse(10);
sample->empty()==1?cout<<"Stack is empty!"<<endl:cout<<"Stack is not empty!"<<endl;
cout<<"Size of the Stack is:"<<sample->sizeofstack()<<endl;
cout<<"Top of stack is:"<<sample->gettop()<<endl;
for(i=1;i<=5;++i)sample->pop();
sample->traverse(10);
sample->empty()==1?cout<<"Stack is empty!"<<endl:cout<<"Stack is not empty!"<<endl;
cout<<"Size of the Stack is:"<<sample->sizeofstack()<<endl;
cout<<"Top of stack is:"<<sample->gettop()<<endl;
//用堆栈回溯实现的八皇后问题求解
cout<<"八皇后问题解:"<<endl;
queen eightqueen;
eightqueen.Queen(8);
cout<<"链式结构堆栈类的类模板实现及用堆栈类求解八皇后问题"<<endl;
cout<<"编写:089906-36@NUAA Aigui.LIU 2002年10月12日完成";
cin.get();
}//程序结束
分享到:
相关推荐
遗传算法求解n皇后问题
n 皇后问题是一道经典的回溯算法问题,其目标是在一个 � × � n×n 的棋盘上放置 � n 个皇后,使得任意两个皇后都不能在同一行、同一列或同一斜线上。 栈可以用来辅助实现回溯算法,本质上就是手动维护了递归...
N皇后问题求解,分别是递归方法实现和非递归方法实现,后者采用回溯方法,C语言实现的
N皇后问题,完全原创,感觉挺不错的,哈哈
算法设计作业,用c++编写的,回溯法求解n皇后问题 运行环境VC6.0
用爬山法解决N皇后问题,3000个皇后可以在1s内求得一个解
八皇后问题的C++算法,可以求解任意N维皇后问题。谢谢下载
GA算法求解n皇后问题。即如何能够在 n×n 的国际象棋棋盘上放置n个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行、纵行或斜线上。
在n*n格的棋盘上放置彼此不受攻击的n个皇后。按照国际向其的规则,皇后可以攻击同一行、同一列、同一斜线上的棋子。
这是一段描述怎样解决N皇后问题的源代码,希望会对你有所帮助,仅代表个人想法,有错请指正
算法n皇后排列树代码 一、 理解回溯法深度优先搜索策略 掌握用回溯法解题的算法框架: (1)递归回溯 (2)子集树算法框架 (3)迭代回溯 (4)排列树算法框架 二、实验内容: 问题描述 用排列树实现8皇后问题 ...
此过程使用回溯算法求出在一个n*n棋盘上放置n个皇后,使其任意两个皇后即不同行,也不同列,也不在同一斜角线上
使用分支限界法解决N皇后问题。因为是广度优先,而且占用比较多的额外空间,所以并不是解N皇后问题的很好的算法,主要是理解分支限界法的使用。
分别用随机算法和回溯法求解N皇后问题 附有详细C++源代码
用C++实现N皇后的确定算法、随机算法、随机回溯算法等高级算法实现N皇后问题
回溯法求解n皇后问题,n皇后问题是一个非常有意思的游戏
自己根据拉斯维加斯算法,写的一个用来求解八皇后问题的python程序,其中可以自定义棋盘大小,显示程序的执行时间。
C语言实现N皇后问题非递归求解 ---- Word版本。
本算法是根据经典的八皇后的问题提出来的,采用了递归回溯法解决问题。
采用随机重启爬山法、最小冲突法和遗传算法求解n皇后问题 可以直接运行,C++编写,效率很快,C++编写,效率很快