详见-算法之美-p180.

#include <iostream>
#include <memory.h>
#include <conio.h>
#include <stdlib.h> using namespace std; /*
92 solution.
* * * * * * * Q
* * * Q * * * *
Q * * * * * * *
* * Q * * * * *
* * * * * Q * *
* Q * * * * * *
* * * * * * Q *
* * * * Q * * *
*/ class QueenPuzzle
{
public:
QueenPuzzle(int _n); public:
void printOut();
void QueenDFS(int _n);
int IsValidate(int _n); private:
int sum;
int max;
int * queen;
}; QueenPuzzle::QueenPuzzle(int _n)
{
this->sum = 0;
this->max = _n;
this->queen = new int[this->max]();
} void QueenPuzzle::printOut()
{
for(int i = 0; i < this->max; i++)
{
for(int j = 0; j < this->max; j++)
{
if(j == this->queen[i])
cout << "Q ";
else
cout << "* ";
}
cout << endl;
} cout << endl << "Enter any key to continue, Q key exit:" << endl << endl;
if(getch() == 'q') exit(0);
} void QueenPuzzle::QueenDFS(int _n)
{
if(_n == this->max)
{
sum++;
cout << endl << sum << " solution." << endl;
this->printOut();
return;
} for(int i = 0; i < this->max; i++)
{
this->queen[_n] = i; if(IsValidate(_n))
{
this->QueenDFS(_n + 1);
}
}
} int QueenPuzzle::IsValidate(int _n)
{
for(int i = 0; i < _n; i++)
{
if(this->queen[i] == this->queen[_n])
return 0; if(abs(this->queen[i] - this->queen[_n]) == (_n - i))
return 0;
} return 1;
} int main()
{
QueenPuzzle queen(8);
queen.QueenDFS(0); return 0;
}

  

QueenPuzzle-N皇后问题的更多相关文章

  1. 递归实现n(经典的8皇后问题)皇后的问题

    问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...

  2. 八皇后算法的另一种实现(c#版本)

    八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...

  3. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. [LeetCode] N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  5. N皇后问题—初级回溯

    N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...

  6. 数据结构0103汉诺塔&八皇后

    主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...

  7. N皇后问题

    题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...

  8. LeetCode:N-Queens I II(n皇后问题)

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...

  9. 八皇后问题_Qt_界面程序实现

    //核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...

  10. 两个NOI题目的启迪8皇后和算24

    论出于什么原因和目的,学习C++已经有一个星期左右,从开始就在做NOI的题目,到现在也没有正式的看<Primer C++>,不过还是受益良多,毕竟C++是一种”低级的高级语言“,而且NOI ...

随机推荐

  1. Python 实现进程间通信(网络编程)

    [网络编程] 1):网络编程是什么意思,网络编程指的是不同机器之间通过网编相互发信息,我们常用的“QQ”,“微信”,“邮箱” 都个网编编程的应用: 网编编程在技术上还有另一个叫法叫“进程间通信”,进程 ...

  2. MySQL的DDL语句、DML语句与DCL语句

    背景:近几年,开源数据库逐渐流行起来.由于具有免费使用.配置简单.稳定性好.性能优良等优点,开源数据库在中低端应用上占据了很大的市场份额,而 MySQL 正是开源数据库中的杰出代表.MySQL 数据库 ...

  3. flask中路由的本质源码分析

    flask中url的本质: 吧url和视图函数封装到一个Rule对象里面去了,并且吧这个对象添加到url_map中 Rule={"url":'/index','method':'i ...

  4. ios总结2018

      1.为什么说Objective-C是一门动态的语言? 1.object-c类的类型和数据变量的类型都是在运行是确定的,而不是在编译时确定.例如:多态特性,我们可以使用父类指针来指向子类对象,并且可 ...

  5. android 圆角编写(懒得去找,写给自己看的)

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="r ...

  6. 腾讯云服务器 - 安装redis3.2.9以及集群

    redis大家都知道,服务器上必不可少的,那么在生产环境下安装的步骤和虚拟机里也是差不多的 官网上最新稳定版是3.2.9,而4.0的更新比较大,但是比几个还是beta版嘛 下载并且上传压缩包至云服务器 ...

  7. Codeforces Round #Pi (Div. 2)(A,B,C,D)

    A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...

  8. markModified声明要修改的数组字段

    更新一个文档的字段的时候,如果该字段的类型是数组类型,则必须在更新保存前声明一下这个数组字段要被修改,否则这个数组字段的值不会被修改.如 article.markModified('categorys ...

  9. [Windows Azure] Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5.

    Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5. T ...

  10. ES6,新增数据结构Set的用法

    ES6 提供了新的数据结构 Set. 特性 似于数组,但它的一大特性就是所有元素都是唯一的,没有重复. 我们可以利用这一唯一特性进行数组的去重工作. 单一数组的去重. let set6 = new S ...