回溯算法解决八皇后

4-皇后问题 (4 - Queen's problem)

In 4- queens problem, we have 4 queens to be placed on a 4*4 chessboard, satisfying the constraint that no two queens should be in the same row, same column, or in same diagonal.

4个皇后问题中 ,我们将4个皇后放置在4 * 4棋盘上,满足了以下约束:两个皇后不应位于同一行,同一列或同一对角线。

The solution space according to the external constraints consists of 4 to the power 4, 4-tuples i.e., Si = {1, 2, 3, 4} and 1<= I <=4, whereas according to the internal constraints they consist of 4! solutions i.e., permutation of 4.

根据外部约束,解空间由4乘以4、4个元组,即Si = {1,2,3,4}1 <= I <= 4 ,而根据内部约束,它们包括4! 解,即4的排列。

回溯的帮助下4 – Queen's的解决方案 (Solution of 4 – queen’s with the help of backtracking)

We can solve 4-queens problem through backtracking by taking it as a bounding function .in use the criterion that if (x1, x2, ……., xi) is a path to a current E-node, then all the children nodes with parent-child labelings x (i+1) are such that (x1, x2, x3, ….., x(i+1)) represents a chessboard configuration in which no queens are attacking.

我们可以通过将其作为边界函数来通过回溯来解决4皇后问题。使用以下准则:如果(x1,x2,……。,xi)是当前E节点的路径,则所有具有父子标签x(i + 1)表示(x1,x2,x3,…..,x(i + 1))表示没有皇后攻击的棋盘配置。

So we start with the root node as the only live node. This time this node becomes the E-node and the path is (). We generate the next child. Suppose we are generating the child in ascending order. Thus the node number 2 is generated and path is now 1 i.e., the queen 1 is placed in the first row and in the first column.

因此,我们从根节点作为唯一的活动节点开始。 这次,该节点成为E节点,路径为()。 我们生成下一个孩子。 假设我们以升序生成子代。 因此,生成了节点编号2,并且路径现在为1,即,将女王1放置在第一行和第一列中。

Now, node 2 becomes the next E-node or line node. Further, try the next node in the ascending nodes i.e., the node 3 which is having x2 = 2 means queen 2 is placed in the second column but by this the queen 1 and 2 are on the same diagonal, so node 3 becomes dead here so we backtrack it and try the next node which is possible.

现在,节点2成为下一个E节点或行节点。 此外,尝试升序节点中的下一个节点,即具有x2 = 2的节点3意味着将女王2放置在第二列中,但由此女王1和2在同一对角线上,因此节点3在此处变为无效因此我们回溯它并尝试下一个可能的节点。

Here, the x2 = 3 means the queen 2 is placed in the 3rd column. As it satisfies all the constraints so it becomes the next live node.

在此,x2 = 3表示将女王2放在第三列中。 由于它满足所有约束,因此它成为下一个活动节点。

After this try for next node 9 having x3 = 2 which means the queen 3 placed in the 2nd column, but by this the 2 and 3 queen are on the same diagonal so it becomes dead. Now we try for next node 11 with x3 = 4, but again the queens 2 and 3 are on the same diagonal so it is also a dead node.

在此之后,尝试下一个具有x3 = 2的下一个节点9,这意味着将女王3放置在第二列中,但是这样2和3女王就在同一对角线上,因此变为死角。 现在我们尝试使用x3 = 4的下一个节点11,但女王2和3同样在对角线上,因此它也是一个死节点。

* The B denotes the dead node.

* B表示死节点。

We try for all the possible positions for the queen 3 and if not any position satisfy all the constraints then backtrack to the previous live node.

我们尝试为女王3确定所有可能的位置,如果没有任何位置满足所有约束,则回溯到先前的活动节点。

Now, the node13 become the new live node with x2 = 4, means queen 2 is placed in the 4th column. Move to the next node 14. It becomes the next live node with x3 = 2 means the queen 3 is placed in the 2nd column. Further, we move to the next node 15 with x4 = 3 as the live node. But this makes the queen 3 and 4 on the same diagonal resulting this node 15 is the dead node so we have to backtrack to the node 14 and then backtrack to the node 13 and try the other possible node 16 with x3 = 3 by this also we get the queens 2 and 3 on the same diagonal so the node is the dead node.

现在,node13成为x2 = 4的新活动节点,这意味着将女王2放置在第4列中。 移至下一个节点14。它将成为x3 = 2的下一个活动节点,这意味着将女王3置于第二列。 此外,我们将x4 = 3作为活动节点移至下一个节点15。 但是,这使得皇后3和4在同一对角线上,导致该节点15是死节点,因此我们必须回溯到节点14,然后回溯到节点13,并以此尝试使用x3 = 3的另一个可能的节点16我们将皇后2和3放在同一对角线上,因此该节点为死节点。

So we further backtrack to the node 2 but no other node is left to try so the node 2 is killed so we backtrack to the node 1 and try another sub-tree having x1 = 2 which means queen 1 is placed in the 2nd column.

因此,我们进一步回溯到节点2,但没有其他节点可以尝试,因此节点2被杀死,因此我们回溯到节点1,并尝试另一个具有x1 = 2的子树,这意味着女王1被放置在第二列中。

Now again with the similar reason, nodes 19 and 24 are killed and so we try for the node 29 with x2 = 4 means the queen 2 is placed in the 4th column then we try for the node 30 with x3 = 1 as a live node and finally we proceed to next node 31 with x4 = 3 means the queen 4 is placed in 3rd column.

现在再次出于类似的原因,节点19和24被杀死,因此我们尝试将x2 = 4的节点29放置在第4列中,然后将x3 = 1的节点30视为活动节点最后我们以x4 = 3进入下一个节点31,这意味着将女王4放在第三列。

Here, all the constraints are satisfied, so the desired result for 4 queens is {2, 4, 1, 3}.

此处,所有约束均得到满足,因此4个皇后的期望结果为{2,4,1,3}。

翻译自: https://www.includehelp.com/algorithms/4-queens-problem-and-solution-using-backtracking-algorithm.aspx

回溯算法解决八皇后

回溯算法解决八皇后_4皇后问题和使用回溯算法的解决方案相关推荐

  1. 人工智能实现a*算法解决八数码_小白带你学回溯算法

    微信公众号:小白算法 关注可了解更多算法,并能领取免费资料.问题或建议,请公众号留言;小白算法,简单白话算法,每个人都能看懂的算法 上一期算法回顾--贪婪法:https://mp.weixin.qq. ...

  2. A*算法解决八数码问题 Java语言实现

    A*算法解决八数码问题 Java语言实现 参考文章: (1)A*算法解决八数码问题 Java语言实现 (2)https://www.cnblogs.com/beilin/p/5981483.html ...

  3. 题目2:隐式图的搜索问题(A*算法解决八数码)

    数据结构课程实践系列 题目1:学生成绩档案管理系统(实验准备) 题目2:隐式图的搜索问题(A*算法解决八数码) 题目3:文本文件单词的检索与计数(实验准备) 文章目录 数据结构课程实践系列 题目1:学 ...

  4. Python利用A*算法解决八数码问题

    资源下载地址:https://download.csdn.net/download/sheziqiong/86790565 资源下载地址:https://download.csdn.net/downl ...

  5. Astar、A星算法解决八数码问题--python实现

    一.问题描述 数码问题又称9宫问题,与游戏"华容道"类似.意在给定的3*3棋格的8个格子内分别放一个符号,符号之间互不相同,余下的一格为空格.并且通常把8个符号在棋格上的排列顺序称 ...

  6. Java实现递归回溯,解决八皇后问题,数据结构与算法

    文章目录 八皇后问题 解决思路 代码实现 运行结果 八皇后问题 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8X8格的国际象棋上摆 ...

  7. Python:各种算法解决八皇后问题

    文章目录 1 八皇后问题 2 解决方案 3 性能对比 4 总结 1 八皇后问题 问题:有一个8乘8的棋盘,现在要将八个皇后放到棋盘上,满足:对于每一个皇后,在自己所在的行.列.两个对角线都没有其他皇后 ...

  8. Nilsson's sequence score算法解决八数码问题解释

    解决了最近一个人工智能关于解决八数码难题的作业. 图可能看不清,除了黑块外其他位置是英文字母ABCDEFGH A*:f(n)=g(n)+h(n) 其中f为总花费,g为已知花费(深度),h为估计花费 关 ...

  9. 全局择优搜索、A*算法、宽度优先算法解决八数码问题

    1问题描述 使用盲目搜索中的宽度优先搜索算法或者使用启发式搜索中的全局择优搜索或A算法,对任意的八数码问题给出求解结果.例如:对于如下具体的八数码问题: 通过设计启发函数,编程实现求解过程,如果问题有 ...

最新文章

  1. opencv滤波美颜
  2. python怎么导入包-Python模块导入与包构建最佳实践
  3. GridView里面的HyperLink和ButtonField操作总结
  4. JQuery 总结(5) 总结各种小应用
  5. 如何在C++Builder中使用全局变量
  6. 性能优化 = 改改代码?
  7. 将来不当科学家,今天不必做科研?
  8. cf1107e uva10559区间dp升维
  9. excel批量导入数据
  10. jQuery addClass
  11. VM虚拟机Bridge模式VMnet0网卡无法启动问题的解决
  12. K均值算法(K_means)
  13. 实体消歧方法(1)__BOOTLEG
  14. 独家对话阿里副总裁李飞飞:数据库的进化之路
  15. mysql远程备份_mysql实现自动远程备份一办法
  16. 搞定 WeakHashMap 的工作原理一篇文章就够了!!!
  17. Enigma机密码加密解密的实现
  18. Vue:自定义组件引入单页面+动态绑定图片
  19. matlab 图片制作动画制作,MATLAB作图之制作动画:单摆运动仿真
  20. matlab surf 坐标设置,matlab中3D曲面函数surf的坐标问题

热门文章

  1. git登录相关操作梳理
  2. mysql判断是否为null_MySQL如何判断字段是否为null
  3. vs中四点画矩形的算法_中考热点,初高中衔接之倒角利器四点共圆
  4. mysql存储map数据结构_map数据结构
  5. android+联系服务器时间,android配置时间服务器+亚洲主要的授时服务器
  6. eplan单线原理图多线原理图_EPLAN-黑盒-2
  7. python读取字典元素笔记_Python 学习笔记 - 字典
  8. Shell脚本——变量
  9. linux scp传输文件命令
  10. leetcode 121 股票买卖问题系列