详见-算法之美-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. vue-client脚手架使用

    1.必须安装node环境 2.安装vue-client脚手架,在命令提示符中运行npm install -g vue-cli 3.创建项目 vue init 模板名 项目名 例如:vue init w ...

  2. JavaScript DOM API初步(整理)

    文档对象模型 文档对象模型(Doucment Object Model,DOM)是表示文档(如HTML文档.XML文档)和访问.操作构成文档的各种元素的应用程序接口.在DOM中,HTML文档的层次结构 ...

  3. 如何使你的Android应用记住曾经使用过的账户信息

    原文:http://android.eoe.cn/topic/android_sdk 当您记住他们的名字时,每个人都会很喜欢.最简单的一个例子,您能够做的,让您的应用更加受人喜爱的,最有效的方法是记住 ...

  4. Eclipse 不能build, pom文件上面有叉叉 解决办法

    Error message:   [html] view plaincopy execution not covered by lifecycle configuration: org.apache. ...

  5. 解决 slf4j + log4j 在云服务上打印乱码

    由于云服务器的环境是纯英文的 虽然在eclipse中可以打印日志显示中文,但是实用putty的时候查看却是乱码,下载日志也同样是乱码 那么只要设置utf-8即可

  6. Unity中yield return null和yield return WaitForEndOfFrame的区别

    2017/07/04修改 - 对WaitForEndOfFrame的LateUpdate时序进行说明. 测试结论: 1.如果只是等待下一帧执行,用yield return null即可.调用顺序在Up ...

  7. Java常考面试题(一)

    序言 我是一只乱飞乱撞的菜鸟,写的文章目前是以记录自己学习,和方便以后查看,期待日后不久能通过自己的努力,获得一点小小的成功,然后写出我的学习经验总结性文章来. ---WH 一.什么是虚拟机?为什么J ...

  8. LeetCode263——Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  9. /etc/ssh/sshd_config 关建字:AllowUsers root test01

    新加用户,在AllowUsers 新增的用户名,重启sshd后,新增的用户才能登录服务器.

  10. 2-7-集合运算(A-B)∪(B-A)-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第2章  线性表 - 集合运算(A-B)∪(B-A) ——<数据结构>-严蔚敏.吴伟民版        ★有疑问先阅读★ 源码使用说明  链接☛☛☛ <数据结构-C语言 ...