博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
回溯算法解决八皇后_4皇后问题和使用回溯算法的解决方案
阅读量:2531 次
发布时间:2019-05-11

本文共 4464 字,大约阅读时间需要 14 分钟。

回溯算法解决八皇后

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同样在对角线上,因此它也是一个死节点。

4 Queen’s problem and solution using backtracking algorithm

* 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确定所有可能的位置,如果没有任何位置满足所有约束,则回溯到先前的活动节点。

4 Queen’s problem and solution

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}。

翻译自:

回溯算法解决八皇后

转载地址:http://dyxzd.baihongyu.com/

你可能感兴趣的文章
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>