日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 回溯算法解决八皇后_4皇后问题和使用回溯算法的解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

回溯算法解決八皇后

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皇后问题和使用回溯算法的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。