回溯算法解决八皇后
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
回溯算法解决八皇后