簡體   English   中英

在C ++中使用回溯算法制作0h h1

[英]Making 0h h1 using backtracking algorithm in c++

因此,當我嘗試閱讀和理解回溯算法的次數很多時,我似乎總是失敗了。

我有一個制作0h h1腦游戲的項目

C ++中的求解器使用回溯算法輸出所有可能的解決方案。

關於游戲規則的快速介紹,您有一個網格(例如6x6),並且必須用相等數量的紅色和藍色方塊填充每個行和列,同時請記住每個列/行都需要不同於其他,並且您不能將三個正方形設置為相同的顏色。

現在,我可以將上述條件用作函數,以使用它們在主求解器函數中測試我的解決方案,但是我無法制定求解算法

該算法應與此類似:

    void try(int i)
   {
     for (int k=0;k<m-1;k++){
     select k-th candidate;
     if (acceptable){
       record it;
       if (i<n)
           try(i+1);
       else
           print solution;
       cancel recording;
        }           
     }
   }

知道怎么做嗎? 謝謝! 希望我的解釋清楚!

解決此問題的簡單方法如下:

backtrack(row,col):
   if table is full: found the solution
   table[row][col] = blue // put blue here
   if no contradictions:
        backtrack(row,col+1) // or backtrack(row+1,0)
   table[row][col] = red // put red here
   if no contradictions:
        backtrack(row,col+1) // or backtrack(col+1,0)
   table[row][col] = null // clear this place to avoid false contradictions

但是我不得不說這是相當低效的。 對於此類問題,最好使用約束滿足啟發式方法,這樣可以更快地解決問題。 有關更多信息,建議您閱讀Stuart Russel的“ Artificial Intelligence: A Modern ApproachConstraint Satisfaction Problems一章。 您可以通過在互聯網上搜索找到本章的學術幻燈片。 如:
http://www.ra.cs.uni-tuebingen.de/lehre/uebungen/ws13/AI/skript/AI_06_Constraint_Satisfaction_Problems_1-30.pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM