详见-算法之美-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. SpringBoot不使用模板引擎直接返回html

    一.在resource目录下面建立文件夹,里面方静态页面. 路径:src\main\resources\static\page\index.html 访问:http://localhost:8080/ ...

  2. IOS App 后台运行

    使用block的另一个用处是可以让程序在后台较长久的运行.在以前,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beg ...

  3. Window 7 + Ubuntu 双系统安装

    硬件: ThinkPad X260 i5-6200U/8G/480G 当前系统: Window 7 旗舰版 64位 下载 Ubuntu 官网 下载桌面版,当前 Ubuntu 版本为:16.04 镜像安 ...

  4. 彻底删除Cygwin

    cygwin是一个好软件,凝聚了大家很多的心血,在win10下运行的很流畅,远比微软自己搞得那个ubuntu顺手,但它有个小问题,重装系统后,如果原来的cgywin文件夹没有删除的话,你会发现你无法删 ...

  5. eclipse自动切换到debug视图

    原文出自:http://blog.csdn.net/yizhizouxiaqu/article/details/7594502 当弹出"Confir Perspective Switch&q ...

  6. tips: javascript 参数传递含有空格怎么办?

    js 方法传参有时候会遇到空格,空格会报错,因为它会默认空格后是元素 解决方法就是使用 escape 和 unescape html: var title = escape(rowObject.tit ...

  7. 【Unity】12.1 基本概念

    开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 导航网格(Navmesh)是世界坐标系中几何体的简化表示,被游戏代理用来进行全局导航.通常,代 ...

  8. Burpsuite如何抓取使用了SSL或TLS传输的 IOS App流量

    之前一篇文章介绍了Burpsuite如何抓取使用了SSL或TLS传输的Android App流量,那么IOS中APP如何抓取HTTPS流量呢, 套路基本上与android相同,唯一不同的是将证书导入i ...

  9. Delphi对象池MyObjectPool.pas

    对象池一般在服务端使用,所以稳定性是第一的. 欢迎提意见 unit uMyObjectPool; interface uses SyncObjs, Classes, Windows, SysUtils ...

  10. 【驱动】MTD子系统分析

    MTD介绍 MTD,Memory Technology Device即内存技术设备 字符设备和块设备的区别在于前者只能被顺序读写,后者可以随机访问:同时,两者读写数据的基本单元不同. 字符设备,以字节 ...