QueenPuzzle-N皇后问题
详见-算法之美-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皇后问题的更多相关文章
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...
- 八皇后算法的另一种实现(c#版本)
八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [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 ...
- N皇后问题—初级回溯
N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...
- 数据结构0103汉诺塔&八皇后
主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...
- N皇后问题
题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...
- 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 ...
- 八皇后问题_Qt_界面程序实现
//核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...
- 两个NOI题目的启迪8皇后和算24
论出于什么原因和目的,学习C++已经有一个星期左右,从开始就在做NOI的题目,到现在也没有正式的看<Primer C++>,不过还是受益良多,毕竟C++是一种”低级的高级语言“,而且NOI ...
随机推荐
- django -- 对行的更新只有在save调用后才会入库
python3 manage.py shell Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) [GCC 4.2.1 (Apple In ...
- 用“网建”平台发手机短信的C#代码
一直都用这个平台发手机短信的,今天做新项目的时候用到了,但是上来博客搜索不到,只好翻以前的源代码翻了好久才找到了,先记下来,以作备用: using System; using System.Colle ...
- redis-cli 连接远程服务器
# redis-cli -h 10.11.09.10 -p 6379 #注意空格
- Android各版本代号、版本号、API/NDK级别、发布时间及市场份额
Android各版本代号.版本号.API/NDK级别.发布时间及市场份额 代号 版本号 API/NDK级别 发布时间 - O 8.0 API level 26 2017-3-21 牛轧糖 Nougat ...
- 转:Math: Math.atan() 与 Math.atan2() 计算两点间连线的夹角
我们可以使用正切操作将角度转变为斜率,那么怎样利用斜率来转换为角度呢?可以利用斜率的反正切函数将他转换为相应的角度.as中有两个函数可以计算反正切,我们来看一下. 1.Math.atan() Math ...
- NonWindowJoin
package org.apache.flink.table.runtime.join /** * Connect data for left stream and right stream. Bas ...
- Gunicorn使用详解
1.什么是Gunicorn Gunicorn是一个WSGI HTTP服务器,python自带的有个web服务器,叫做wsgiref, Gunicorn的优势在于,它使用了pre-fork worker ...
- django rest_framework入门二-序列化
在前一节中,我们已经粗略地介绍了rest_framework的作用之一序列化,下面我们将详细探究序列化的使用. 1.新建一个app snippets python manage.py startapp ...
- 每日英语:Why Mom's Time Is Different From Dad's Time
Several years ago, while observing a parenting group in Minnesota, I was struck by a confession one ...
- linux命令(53):用户和用户组
Linux 用户和用户组详细解说 本文主要讲述在Linux 系统中用户(user)和用户组(group)管理相应的概念: 用户(user)和用户组(group)相关命令的列举: 其中也对单用户多任务, ...