`
andyliuxs
  • 浏览: 138291 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

n-皇后问题的实现

阅读更多

最近在学习C语言,随便编写实现了一个8-皇后问题,同时他程序也可以扩展为N-皇后问题.

8-皇后问题描述:八皇后问题是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。

源代码:

#include <stdio.h>
#include <math.h>
#define N 8
int X[N];
int NUM=0;
int main()
{
 void print();
 int canPlace(int);
 int i;
 int k = 0;
 X[0] = -1;
 while(k>=0)
 {
  X[k] = X[k]+1;
  while(X[k] < N && !canPlace(k))
   X[k] = X[k]+1;
  if(X[k] < N)//找到位置
  {
   if(k==N-1)//是否为完整的解
    print();
   else
   {
    k++;
    X[k] = -1;
   }
  }
  else
   k--;
 }
 return 0;
}

void print()
{
 int i;
 NUM++;
 printf("the %dth answer is :",NUM);
 for(i=0;i<N;i++)
 {
  printf("%d ",X[i]);
 }
 printf("\n");
}

int canPlace(int k)
{
 int i;
 for(i=0;i<k;i++)
 {
  if(X[k] == X[i] || abs(i-k) == abs(X[k]-X[i]))
   break;
 }
 if(i >= k)
  return 1;
 else
  return 0;
}
结果:

the 1th answer is :0 4 7 5 2 6 1 3
the 2th answer is :0 5 7 2 6 3 1 4
the 3th answer is :0 6 3 5 7 1 4 2
the 4th answer is :0 6 4 7 1 3 5 2
the 5th answer is :1 3 5 7 2 0 6 4
the 6th answer is :1 4 6 0 2 7 5 3
the 7th answer is :1 4 6 3 0 7 5 2
the 8th answer is :1 5 0 6 3 7 2 4
the 9th answer is :1 5 7 2 0 3 6 4
the 10th answer is :1 6 2 5 7 4 0 3
the 11th answer is :1 6 4 7 0 3 5 2
the 12th answer is :1 7 5 0 2 4 6 3
the 13th answer is :2 0 6 4 7 1 3 5
the 14th answer is :2 4 1 7 0 6 3 5
the 15th answer is :2 4 1 7 5 3 6 0
the 16th answer is :2 4 6 0 3 1 7 5
the 17th answer is :2 4 7 3 0 6 1 5
the 18th answer is :2 5 1 4 7 0 6 3
the 19th answer is :2 5 1 6 0 3 7 4
the 20th answer is :2 5 1 6 4 0 7 3
the 21th answer is :2 5 3 0 7 4 6 1
the 22th answer is :2 5 3 1 7 4 6 0
the 23th answer is :2 5 7 0 3 6 4 1
the 24th answer is :2 5 7 0 4 6 1 3
the 25th answer is :2 5 7 1 3 0 6 4
the 26th answer is :2 6 1 7 4 0 3 5
the 27th answer is :2 6 1 7 5 3 0 4
the 28th answer is :2 7 3 6 0 5 1 4
the 29th answer is :3 0 4 7 1 6 2 5
the 30th answer is :3 0 4 7 5 2 6 1
the 31th answer is :3 1 4 7 5 0 2 6
the 32th answer is :3 1 6 2 5 7 0 4
the 33th answer is :3 1 6 2 5 7 4 0
the 34th answer is :3 1 6 4 0 7 5 2
the 35th answer is :3 1 7 4 6 0 2 5
the 36th answer is :3 1 7 5 0 2 4 6
the 37th answer is :3 5 0 4 1 7 2 6
the 38th answer is :3 5 7 1 6 0 2 4
the 39th answer is :3 5 7 2 0 6 4 1
the 40th answer is :3 6 0 7 4 1 5 2
the 41th answer is :3 6 2 7 1 4 0 5
the 42th answer is :3 6 4 1 5 0 2 7
the 43th answer is :3 6 4 2 0 5 7 1
the 44th answer is :3 7 0 2 5 1 6 4
the 45th answer is :3 7 0 4 6 1 5 2
the 46th answer is :3 7 4 2 0 6 1 5
the 47th answer is :4 0 3 5 7 1 6 2
the 48th answer is :4 0 7 3 1 6 2 5
the 49th answer is :4 0 7 5 2 6 1 3
the 50th answer is :4 1 3 5 7 2 0 6
the 51th answer is :4 1 3 6 2 7 5 0
the 52th answer is :4 1 5 0 6 3 7 2
the 53th answer is :4 1 7 0 3 6 2 5
the 54th answer is :4 2 0 5 7 1 3 6
the 55th answer is :4 2 0 6 1 7 5 3
the 56th answer is :4 2 7 3 6 0 5 1
the 57th answer is :4 6 0 2 7 5 3 1
the 58th answer is :4 6 0 3 1 7 5 2
the 59th answer is :4 6 1 3 7 0 2 5
the 60th answer is :4 6 1 5 2 0 3 7
the 61th answer is :4 6 1 5 2 0 7 3
the 62th answer is :4 6 3 0 2 7 5 1
the 63th answer is :4 7 3 0 2 5 1 6
the 64th answer is :4 7 3 0 6 1 5 2
the 65th answer is :5 0 4 1 7 2 6 3
the 66th answer is :5 1 6 0 2 4 7 3
the 67th answer is :5 1 6 0 3 7 4 2
the 68th answer is :5 2 0 6 4 7 1 3
the 69th answer is :5 2 0 7 3 1 6 4
the 70th answer is :5 2 0 7 4 1 3 6
the 71th answer is :5 2 4 6 0 3 1 7
the 72th answer is :5 2 4 7 0 3 1 6
the 73th answer is :5 2 6 1 3 7 0 4
the 74th answer is :5 2 6 1 7 4 0 3
the 75th answer is :5 2 6 3 0 7 1 4
the 76th answer is :5 3 0 4 7 1 6 2
the 77th answer is :5 3 1 7 4 6 0 2
the 78th answer is :5 3 6 0 2 4 1 7
the 79th answer is :5 3 6 0 7 1 4 2
the 80th answer is :5 7 1 3 0 6 4 2
the 81th answer is :6 0 2 7 5 3 1 4
the 82th answer is :6 1 3 0 7 4 2 5
the 83th answer is :6 1 5 2 0 3 7 4
the 84th answer is :6 2 0 5 7 4 1 3
the 85th answer is :6 2 7 1 4 0 5 3
the 86th answer is :6 3 1 4 7 0 2 5
the 87th answer is :6 3 1 7 5 0 2 4
the 88th answer is :6 4 2 0 5 7 1 3
the 89th answer is :7 1 3 0 6 4 2 5
the 90th answer is :7 1 4 2 0 6 3 5
the 91th answer is :7 2 0 5 1 4 6 3
the 92th answer is :7 3 0 2 5 1 6 4

分享到:
评论

相关推荐

    c++代码运用回溯与位运算算法实现N-皇后问题

    本资源使用c++代码实现N-皇后问题并附上研究小论文,实现算法有:回溯法(递归),回溯法(递归)的镜像优化,回溯法(非递归),回溯法(非递归)的镜像优化,位运算算法,位运算算法的镜像优化。N-皇后问题是八皇后问题的...

    N皇后问题的实现

    N皇后问题的实现

    n皇后问题c++实现

    n皇后问题C++实现 n皇后问题是计算机科学中的一种经典问题,旨在寻找一种算法,可以将n个皇后摆放在n*n的棋盘上,使得每个皇后不在同一行、同一列或同一斜线上。该问题的解决需要使用回溯法,首先初始化一个一维...

    N皇后问题实现

    N皇后问题的3种实现方案

    N皇后问题 C++实现

    N皇后问题,在一个N×N国际象棋盘 上,有N个皇后,每个皇后占一格;要求皇后间不会出现 相互“攻击”的现象,即不能有两个皇后处在同一行、同一 列或同一对角线上。问共有多少种不同的方法。下图以4 个皇后为例,...

    MFC实现n皇后算法(附报告)

    在本项目中,我们主要探讨的是“MFC实现n皇后算法”,这是一个经典的计算机科学问题,源自于著名的八皇后问题。八皇后问题要求在8×8的棋盘上摆放8个皇后,使得任意两个皇后都无法在同一行、同一列或对角线上相互...

    NQueen N皇后问题java实现

    总之,N皇后问题的Java实现是一个涉及回溯算法、面向对象编程和图形化界面设计的综合性实例,对于提升编程技能和理解算法有着显著的帮助。通过学习和实践这样的项目,开发者不仅可以掌握基本的编程技巧,还能深入...

    n皇后的c语言实现

    n皇后的c语言实现

    n皇后问题--回溯法实验

    **源代码文件“n皇后问题.cpp”是实现该问题的程序文件。在C++中,我们通常会定义一个函数,比如`solveNQueens`,来处理这个任务。此函数的核心思想是递归地尝试在每个位置放置皇后,并检查是否符合要求。如果当前...

    回溯法解决N皇后问题 Java代码实现

    通过GUI实现N皇后问题的动态演示,可以使用Java Swing或JavaFX库创建用户界面,展示皇后在棋盘上的位置,并允许用户选择不同数量的皇后进行演示。这样,用户可以直观地看到回溯法如何在棋盘上逐步尝试和撤销皇后的...

    n皇后问题实验报告

    ### N皇后问题实验报告知识点解析 #### 一、实验目的及要求 本次实验的主要目的是让学生通过实际编程操作,深入理解并解决N皇后问题。实验要求包括以下几点: 1. **了解皇后相互攻击的条件**:当两个皇后位于同一...

    N皇后问题摆法算法描述

    在给定的文件列表中,我们可以看到一些可能的源代码文件,如`CQueen.cpp`、`QueenNView.cpp`、`QueenN.cpp`等,这些文件可能包含了N皇后问题的实现。例如,`CQueen.cpp`可能包含皇后类的实现,`QueenNView.cpp`可能...

    回溯法实现N皇后问题

    **回溯法实现N皇后问题** N皇后问题是一个经典的计算机科学问题,它的目标是在一个N×N的棋盘上放置N个皇后,使得任意两个皇后都不能在同一行、同一列或同一对角线上。这是一个典型的约束满足问题,可以利用回溯法...

    N皇后问题-java实现

    可以相比传统N皇后解决增加了运行速度,因为采用二进制进行皇后位置运算,其中图片位置以及图片请自行替换。

    用栈的n皇后问题源码+流程图

    在C语言中,我们可以利用栈数据结构来辅助实现n皇后问题的解决方案。栈是一种后进先出(LIFO)的数据结构,非常适合用于深度优先搜索,因为它可以方便地回溯到上一步以尝试其他可能的路径。 首先,我们需要创建一个...

    n-queens-visualizer, 在反应&通量中,对N 皇后问题可视化的解决.zip

    n-queens-visualizer, 在反应&通量中,对N 皇后问题可视化的解决 n局部搜索算法的可视化探索queens皇后问题的解。 反应岩石的特征。查看实况:https://haseeb-qureshi.github.io/n-queens-visualizer 用不同的局部...

    利用回溯法解决n皇后问题

    在这个场景中,我们关注的是“n皇后问题”。n皇后问题是在一个n×n的棋盘上放置n个皇后,要求任何两个皇后不能处于同一行、同一列或同一对角线上。** **C++是实现这一算法的理想选择,它是一种静态类型的、编译式的...

    N皇后问题 C/C++实现

    5. **代码实现**:`main.cpp`文件应该包含了主函数,它调用了处理N皇后问题的递归函数。程序的入口通常会先读取N的值,然后调用N皇后问题的解决方案,并输出结果。在`N皇后问题.exe`中,这个编译后的可执行文件是...

    C#实现-回溯求解-N皇后

    通过这样的回溯实现,我们可以找到N皇后问题的所有解或者仅找出一个解,具体取决于代码的实现。回溯算法的优势在于其能够高效地搜索问题空间,避免无效的计算,从而在有限的时间内找到问题的解决方案。对于N皇后问题...

Global site tag (gtag.js) - Google Analytics