详见-算法之美-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. Redis设置和更新Key的过期时间

    EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除. 在 Redis 中,带有生存时间的 key 被称为『易失的』(volati ...

  2. DevExpress GridControl List绑定方式下新增行的方法

    List<Person> gridDataList = new List<Person>(); //此处是数据源 List集合 BindingList<Person> ...

  3. 【Unity】3.1 利用内置的3D对象创建三维模型

    分类:Unity.C#.VS2015 创建日期:2016-04-02 一.基本概念 Unity已经内置了一些基本的3D对象,利用这些内置的3D对象就可以直接构建出各种3D模型(当然,复杂的三维模型还需 ...

  4. NGINX宏观手记(变量|配置)

    前言 任何一个工具都有它的灵魂所在,作为一个PHP程序员,我们可能仅仅使用了它的一小部分,这篇文章让你更加了解Nginx,本章大多都是总结.翻译.整理 ,希望你可以知道nginx不仅仅是PHP的附属品 ...

  5. 关于CAE的那点儿破事儿【二】

    前面在<关于CAE的那点破事儿>一文中,主要提到了CAE是什么.CAE能做些什么.人在CAE应用中的作用以及CAE从业中应当具有哪些基本素质.然而CAE是一把双刃剑,如果不能在工程应用中很 ...

  6. CMD命令下访问Oracle数据库

    1.非集群下 Windows环境下数据库 127.0.0.1 只是个IP代表  实际要输入你要访问的数据库服务器IP地址的 如果数据库服务器不在本机上,需要加上数据库服务器的地址:用户名/密码@IP地 ...

  7. Android 网络请求get/post工具类:NetUtil

    package com.net.util;   import java.io.IOException; import java.io.UnsupportedEncodingException; imp ...

  8. 【转】(四)unity4.6Ugui中文教程文档-------概要-UGUI Visual Components

    原创至上,移步请戳:(四)unity4.6Ugui中文教程文档-------概要-UGUI Visual Components 3.Visual Components 有新的组件和游戏对象已添加到uG ...

  9. netty的线程池-----揭示了使用两个线程池的原因

    线程模型是Netty的核心设计,设计地很巧妙,之前项目中有一块处理并发的设计和Netty的Eventloop单线程设计类似,效果得到了实证. Netty5的类层次结构和之前的版本变化很大,网上也有很多 ...

  10. Python学习笔记(4):容器、迭代对象、迭代器、生成器、生成器表达式

    在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set,dict ...