This commit is contained in:
krahets
2024-05-04 19:57:03 +08:00
parent 6d966b8b5d
commit 2395804410
27 changed files with 792 additions and 91 deletions
@@ -26,7 +26,7 @@ comments: true
也就是說,我們可以採取逐行放置策略:從第一行開始,在每行放置一個皇后,直至最後一行結束。
圖 13-17 所示為 $4$ 皇后問題的逐行放置過程。受畫幅限制,圖 13-17 僅展開了第一行的其中一個搜尋分支,並且將不滿足列約束和對角線約束的方案都進行了剪枝。
圖 13-17 所示為 4 皇后問題的逐行放置過程。受畫幅限制,圖 13-17 僅展開了第一行的其中一個搜尋分支,並且將不滿足列約束和對角線約束的方案都進行了剪枝。
![逐行放置策略](n_queens_problem.assets/n_queens_placing.png){ class="animation-figure" }
@@ -269,6 +269,7 @@ comments: true
}
*res = append(*res, newState)
return
}
// 走訪所有列
for col := 0; col < n; col++ {
@@ -724,7 +724,7 @@ comments: true
(*selected)[i] = true
*state = append(*state, choice)
// 進行下一輪選擇
backtrackI(state, choices, selected, res)
backtrackII(state, choices, selected, res)
// 回退:撤銷選擇,恢復到之前的狀態
(*selected)[i] = false
*state = (*state)[:len(*state)-1]