【本文链接】

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. 输入年月,输出月份有几天(分别用了if——else和switch)

    首先是switch做的 class Program { static void Main(string[] args) {/* 题目要求:请用户输入年份,输入月份,输出该月的天数. 思路:一年中月份的 ...

  2. bzoj 1458 网络流

    我们可以知道每行最多可以有多少个格子不用建点,设为x[i],每列同理设为y[i],那么我们连接(source,i,x[i]),(i,sink,y[i])表示我们将一个格子不建点,那么(i,j,flag ...

  3. Bzoj1150 数据备份Backup

    Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家 ...

  4. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  5. 轻量级应用开发之(03)UIVIew

    本文是我在学习OC中的一些经验总结,在学习中总结了常用的Mac技巧,欢迎群友对本文提出意见,如有问题请联系我. 一 九宫格-购物车 通过文件加载数据: NSString * file = [[NSBu ...

  6. Java线程中run和start方法的区别

    http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...

  7. jquery------.resizable()的使用

    index.jsp //加上这两行代码,右下角会有样式效果<link rel="stylesheet" href="//code.jquery.com/ui/1.1 ...

  8. epoch iteration batchsize

    深度学习中经常看到epoch. iteration和batchsize,下面按自己的理解说说这三个的区别: (1)batchsize:批大小.在深度学习中,一般采用SGD训练,即每次训练在训练集中取b ...

  9. css absolute与relative的区别

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 操作sqlserver数据库常用的三个方法

    1. ADO.NET -> 连接字符串,常用的两种方式: server=计算机名或ip\实例名;database=数据库名;uid=sa;pwd=密码; server=计算机名或ip\实例名;d ...