【本文链接】

http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html

题目】

在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行、同一列或者同一对角斜线上。下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法。请求出总共有多少种摆法。

分析

之前博文28.字符串的排列[StringPermutation]介绍过字符串的全排列,八皇后问题也可以转换为全排列问题。

由于八个皇后的任意两个不能处在同一行,那么每一个皇后占据一行。于是我们可以定义一个数组pos[8],pos[i]=value表示第i行将皇后放置于value位置。先把pos的八个数字分别用0-7初始化,接下来对数组pos数组做全排列。由于我们是用不同的数字初始化数组中的数字,因此任意两个皇后肯定不同列。我们只需要判断得到的每一个排列对应的八个皇后是不是在同一对角斜线上,也就是数组的两个下标i和j,是不是i-j==pos [i]- pos [j]或者j-i==pos [i]- pos [j]。

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
// 54_EightQueensPuzzle.cpp : Defines the entry point for the console application.
//
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/24
*/

#include "stdafx.h"
#include <iostream>
using namespace std;

;
;

// swap a and b
void Swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

// check whether pos array is valid
bool CheckValid(int *pos, int n)
{
    ; i < n; ++i)
    {
        ; j < n; j++)
        {
            //only need to check whether pos[i] and pos[j] are on diagonal
            if (i - j == pos[i] - pos[j] || j - i == pos[i] - pos[j])
            {
                return false;
            }
        }
    }
    return true;
}

// print pos array
void Print(int *pos, int n)
{
    ; i < n; ++i)
        printf("%d ", pos[i]);
}

void Permutation(int *pos, int n, int index)
{
    m_nPermutationCount++;
    if (index == n)
    {
        // this permutation generated
        if (CheckValid(pos, n))
        {
            // valid permutation
            m_nValidCount++;
            //Print(pos,n);
        }
    }
    else
    {
        for (int i = index; i < n; ++i)
        {
            Swap(pos[index], pos[i]);
            Permutation(pos, n, index + );
            Swap(pos[index], pos[i]);
        }
    }
}

void EightQueensPuzzle()
{
    // init pos
    int pos[N];
    ; i < N; i++)
    {
        pos[i] = i;
    }
    Permutation(pos, N, );
}

void test_main()
{
    EightQueensPuzzle();
    cout << m_nPermutationCount << endl;
    cout << m_nValidCount << endl; //92
}

int _tmain(int argc, _TCHAR *argv[])
{
    test_main();
    ;
}

【参考】

http://www.cnblogs.com/hellogiser/p/3738844.html

http://zhedahht.blog.163.com/blog/static/2541117420114331616329/

http://en.wikipedia.org/wiki/Eight_queens_puzzle

http://blog.csdn.net/hackbuteer1/article/details/6657109

【本文链接】

http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html

54. 八皇后问题[eight queens puzzle]的更多相关文章

  1. Python----递归------Eight Queens 八皇后问题

    递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个 ...

  2. 算法——八皇后问题(eight queen puzzle)之回溯法求解

    八皇后谜题是经典的一个问题,其解法一共有种! 其定义: 首先定义一个8*8的棋盘 我们有八个皇后在手里,目的是把八个都放在棋盘中 位于皇后的水平和垂直方向的棋格不能有其他皇后 位于皇后的斜对角线上的棋 ...

  3. PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT_A1128#N Queens Puzzle

    Source: PAT A1128 N Queens Puzzle (20 分) Description: The "eight queens puzzle" is the pro ...

  5. Python学习二(生成器和八皇后算法)

    看书看到迭代器和生成器了,一般的使用是没什么问题的,不过很多时候并不能用的很习惯 书中例举了经典的八皇后问题,作为一个程序员怎么能够放过做题的机会呢,于是乎先自己来一遍,于是有了下面这个ugly的代码 ...

  6. Python解决八皇后问题

    最近看Python看得都不用tab键了,哈哈.今天看了一个经典问题--八皇后问题,说实话,以前学C.C++的时候有这个问题,但是当时不爱学,没搞会,后来算法课上又碰到,只是学会了思想,应该是学回溯法的 ...

  7. C语言解决八皇后问题

    #include <stdio.h> #include <stdlib.h> /* this code is used to cope with the problem of ...

  8. 八皇后,回溯与递归(Python实现)

    八皇后问题是十九世纪著名的数学家高斯1850年提出 .以下为python语句的八皇后代码,摘自<Python基础教程>,代码相对于其他语言,来得短小且一次性可以打印出92种结果.同时可以扩 ...

  9. 八皇后问题 --- 递归解法 --- java代码

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

随机推荐

  1. Winform中的PictureBox读取图像文件无法释放的问题

    今天做一拍照程序,相机SDK什么都搞定,就为了显示图像并且保存照片的步骤卡了半天. 原因是预览图像使用了PictureBox,载入图片文件的方式为: pictureBoxPhoto.Image = I ...

  2. Java算法-符号&

     &与运算符 与运算符用符号“&”表示,其使用规律如下:两个操作数中位都为1,结果才为1,否则结果为0 例如下面的程序段. public class data13 { public s ...

  3. Oracle之自定义函数

    数据库中函数包含四个部分:声明.返回值.函数体和异常处理. --没有参数的函数 create or replace function get_user return varchar2 is v_use ...

  4. Visio绘制时序图

    用visio建立时序图 1.选择模版 2.常见符号 时序图创建步骤 1.确定交互过程的上下文: 2.识别参与过程的交互对象: 3.为每个对象设置生命线: 4.从初始消息开始,依次画出随后消息: 5.考 ...

  5. java获取每个月的最后一天

    package timeUtil; import java.text.SimpleDateFormat; import java.util.Calendar; public class LastDay ...

  6. python库requests登录zhihu

    废了很大劲,开始搞错了登录post信息的网址,后来没找到xsrf信息,看了很多文章才搞定. 大概过程如下: 打开登录页面,同时打开fldder,输入信息去监控过程. 查看post了哪些信息,哪些是自己 ...

  7. linux内存回收 内核参数

    ss -atu| awk '/^tcp/{++S[$2]} END {for(a in S) print a,S[a]}' ps up pid (RSS:实际内存大小,长驻内存) ps o pid,c ...

  8. yum被锁Another app is currently holding the yum lock; waiting for it to exit...

    可能是系统自动升级正在运行,yum在锁定状态中. 可以通过强制关掉yum进程: #rm -f /var/run/yum.pid 然后就可以使用yum了.

  9. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  10. JavaScript的apply和call方法及其区别

    参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...