N皇后问题

八皇后问题是一个古老而著名的问题,它是回溯算法的典型例题。该问题是十九世纪德国著名数学家高斯于1850年提出的:在8行8列的国际象棋棋盘上摆放着 八个皇后。若两个皇后位于同一行、同一列或同一对角线上,则称为它们为互相攻击。
现在要求使这八个皇后不能相互攻击,即任意两个皇后都不能处于同一行、同一列或同一对角线上,问有多少种摆法。

解题思路

在递归方式中,pos[i]表示第i行的皇后摆在第pos[i]列上。也可以使用循环来模拟递归过程。

实现代码

打印所有摆放方式:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

class Queens {
public:
    int nQueens(int n) {
        vector<int> pos(n, 0);
        int i = 0;
        fun(n, pos, i);
        return 0;
    }

    void fun(int n, vector<int>& pos, int i)
    {
        if (i == n)
        {
            return;
        }
        for (int k = 0; k < n; k++)
        {
            pos[i] = k;
            if (check(pos, i))
            {
                if (i == n - 1)
                {
                    show(pos);
                }
                else
                {
                    fun(n, pos, i + 1);
                }
            }
        }
    }

    bool check(vector<int> pos, int i)
    {
        for (int k = 0; k < i; k++)
        {
            if (pos[k] == pos[i] || abs(k - i) == abs(pos[k] - pos[i]))
            {
                return false;
            }
        }
        return true;
    }

    void show(vector<int> pos)
    {
        for (int i = 0; i < pos.size(); i++)
        {
            for (int j = 0; j < pos.size(); j++)
            {
                if (pos[i] == j)
                {
                    cout<<'+'<<" ";
                }
                else
                {
                    cout<<'0'<<" ";
                }
            }
            cout<<endl;
        }
        cout<<endl;
    }
};

int main()
{
    Queens q;
    q.nQueens(8);
}

打印摆放种类:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

class Queens {
public:
    int nQueens(int n) {
        vector<int> pos(n, 0);
//        递归方式
//        int count = 0;
//        fun(n, pos, 0, count);
//        return count;

//        非递归方式
        return fun2(n, pos);
    }

    //非递归方式
    int fun2(int n, vector<int> pos)
    {
        int i = 0;
        pos[i] = -1;
        int count = 0;
        while (i >= 0)
        {
            pos[i]++;
            while (pos[i] < n && !check(pos, i))
            {
                pos[i]++;
            }
            if (pos[i] < n)
            {
                if (i == n - 1)
                {
                    count++;
                }
                else
                {
                    i++;
                    pos[i] = -1;
                }
            }
            else
            {
                i--;
            }
        }

        return count;
    }

    //递归方式
    void fun(int n, vector<int>& pos, int i, int& count)
    {
        if (i == n)
        {
            return;
        }
        for (int k = 0; k < n; k++)
        {
            pos[i] = k;
            if (check(pos, i))
            {
                if (i == n - 1)
                {
                    count++;
                }
                else
                {
                    fun(n, pos, i + 1, count);
                }
            }
        }
    }

    bool check(vector<int> pos, int i)
    {
        for (int k = 0; k < i; k++)
        {
            if (pos[k] == pos[i] || abs(k - i) == abs(pos[k] - pos[i]))
            {
                return false;
            }
        }
        return true;
    }
};

int main()
{
    Queens q;
    for (int i = 1; i <= 12; i++)
    cout<<q.nQueens(i)<<endl;
}

转载:http://blog.csdn.net/foreverling/article/details/47380027

时间: 2024-08-30 11:47:50

N皇后问题的相关文章

编程c语言-个C++中皇后问题的题。在n*n棋盘中如何摆n个皇后使其互相都不挡道?用java写可以吗!?怎么写?

问题描述 个C++中皇后问题的题.在n*n棋盘中如何摆n个皇后使其互相都不挡道?用java写可以吗!?怎么写? 个C++中皇后问题的题.在n*n棋盘中如何摆n个皇后使其互相都不挡道?用java写可以吗?怎么写? 解决方案 N 皇后问题N个皇后问题!!!M*N棋盘上的K皇后问题 解决方案二: 八皇后问题,java实现 public class Queen8 { public static int num = 0; //累计方案总数 public static final int MAXQUEEN

八皇后问题的C#解答

解答|问题 改编自V星[视窗王子]应答程序,如下:<br><br>using System;<br>class Queen{<br>    const int SIZE = 8;//皇后数<br>    public static void Main()<br>    {<br>        int[] Queen = new int [SIZE];//每行皇后的位置<br>        int y,x,i

皇后问题之C#版(非递归)

递归|问题   /* *Author:Junyi Sun @CCNU* E-mail:fxsjy@yahoo.com.cn*/ using System;namespace sunjoy{    public class Queen    {        public static int Main()        {            int board_size = 0,x=0,y=0;//棋盘大小,当前行,当前列            uint solution_count = 0

UVa 639:Don&#039;t Get Rooked, 类八皇后问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=580 题目类型: 暴力, 回溯法 题目: In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this p

UVa 167:The Sultan&#039;s Successors, 八皇后问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=103 题目类型: 回溯 原题: The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate

N皇后问题摆法算法描述

题目说明: 在一个N×N的国际象棋棋盘中摆N个皇后,使这N个皇后不能互相被对方吃掉. 题目要求: (1)依次输出各种成功的放置方法. (2)最好能画出棋盘的图形形式,并动态的演示试探过程. (3)程序能方便的移植到其它规格的棋盘上. 例如:在一个4×4的棋盘可以摆放的棋位置{(0,1)(1,3)(2,0)(3,2)},{(0,2)(1,0)(2,3)(3,1)}两种. 题目分析: 一.试探过程分析: N×N皇后问题的求解过程就是一个试探回逆的过程.如图-1 (图1-1) 1.首先查找第一行的可放

基于Delphi的八皇后问题动态实现

摘要 对于八皇后问题的实现,如果结合动态的图形演示,则可以使算法的描述更形象.更生动,使教学能产生良好的效果. 关键词 八皇后问题 冲突 数据结构 线程类 八皇后问题是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 下面用delphi6实现的八皇后问题的动态图形程序,能够演示全部的92组解.八皇后问题动态图形的实现,主要应解决以下

常用算法:C#八皇后问题

八皇后问题是一个古老而著名的问题,是回溯算法的典型应用.八皇后问题就是棋盘上的8个皇后不能在同一行.一列或一条斜线上,在8!=40320种排列中共有92种解决方案.代码如下: using System; using System.Collections.Generic; using System.Text;namespace ExQueen { class Queen { public void QueenArithmetic(int size){ int[] Queen = new int[s

python基于右递归解决八皇后问题的方法

  本文实例讲述了python基于右递归解决八皇后问题的方法.分享给大家供大家参考.具体分析如下: 凡是线性回溯都可以归结为右递归的形式,也即是二叉树,因此对于只要求一个解的问题,采用右递归实现的程序要比回溯法要优美的多. ? 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 def Test(queen,n): '''这个就不用说了吧,就是检验第n(下标,0-7)行皇后的位置是否合理''' q=que

javascript递归回溯法解八皇后问题

  javascript递归回溯法解八皇后问题:           网上看到许多关于八皇后算法的文章,很少能看到使用javascript来实现的,今天就给大家使用javascript来解决下这个问题,有需要的小伙伴可以参考下. 下面给大家分享的是回溯法解八皇后, 带详细注解,这里就不多废话了. ? 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