【本文链接】

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. hdu1828 线段树+离散化+扫描线

    添加lb[],rb[]数组,来标记竖边.添加num,来计算竖边的个数,因为计算周长的时候,未覆盖的竖边都要加. #include<stdio.h> #include<stdlib.h ...

  2. SQLServer错误:过程 sp_addextendedproperty,第 xxx 行对象无效。'dbo.xxx.xxx' 不允许有扩展属性,或对象不存在。

    上传数据库到虚拟主机,在执行SQL脚本的时候出现以下的错误: 消息 15135,级别 16,状态 8,过程 sp_addextendedproperty,第 37 行 对象无效.'dbo.Messag ...

  3. POJ1753Flip Game(DFS + 枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37050   Accepted: 16122 Descr ...

  4. KindEditor上传本地图片在ASP.NET MVC的配置

    http://www.cnblogs.com/upupto/archive/2010/08/24/1807202.html 本文解决KindEditor上传本地图片在ASP.NET MVC中的配置. ...

  5. Redis 下载

    https://github.com/MSOpenTech/redis/releases下载 Redis伴侣Redis Desktop Manager:http://redisdesktop.com/ ...

  6. 转 vagrant package[打包命令]详解

    转 vagrant package[打包命令]详解   vagrant的一个非常重要的功能就是在你的同事之间分享你的box从而使大家的开发环境保持同步,打包[package]正是实现这一功能的关键所在 ...

  7. polling 和 long polling 工作原理

    polling & long polling 参考:http://stackoverflow.com/questions/11077857/what-are-long-polling-webs ...

  8. webservice原理

      webservice的工作原理 WebService的主要目标是跨平台的可互操作性.为了达到这一目标,WebService完全基于XML(可扩展标记语言).XSD(XMLSchema)等独立于平台 ...

  9. 开启Mysql远程访问的所有方法

    开启Mysql远程访问的所有方法 http://superyjcqw.blog.163.com/blog/static/16105830520117111040436/ Mysql默认是不可以通过远程 ...

  10. vsftpd 搭建与介绍

    CentOS Linux Vsftp服务器配置 CentOS Linux Vsftp服务器配置 1.开启防火墙ftp端口           vi /etc/sysconfig/iptables    ...