简体   繁体   English

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

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

So as many times as I tried reading and understanding the backtracking algorithm, I always seem to fail. 因此,当我尝试阅读和理解回溯算法的次数很多时,我似乎总是失败了。

I have a project to make 0h h1 brain game 我有一个制作0h h1脑游戏的项目

solver in c++ using backtracking algorithm to output all possible solutions. C ++中的求解器使用回溯算法输出所有可能的解决方案。

a quick introduction about the rules of the game, you have a grid(say 6x6) and you have to fill each of each of its rows and columns with an equal number of red and blue squares, keeping in mind that each column/row needs to be different than the others, and that you can't set three squares with the same color. 关于游戏规则的快速介绍,您有一个网格(例如6x6),并且必须用相等数量的红色和蓝色方块填充每个行和列,同时请记住每个列/行都需要不同于其他,并且您不能将三个正方形设置为相同的颜色。

Now I was able to make the conditions above as functions to use them to test my solutions in the main solver function, yet I wasn't able to make the solving algorithm 现在,我可以将上述条件用作函数,以使用它们在主求解器函数中测试我的解决方案,但是我无法制定求解算法

the algorithm should be similar to this: 该算法应与此类似:

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

Any idea how to do it? 知道怎么做吗? Thanks! 谢谢! and I hope my explanation was clear! 希望我的解释清楚!

The simple backtrack approach for this problem would be something like this: 解决此问题的简单方法如下:

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

But I have to say this is rather inefficient. 但是我不得不说这是相当低效的。 For this kind of problems it is better to use constraint satisfaction heuristics which result in a faster solution. 对于此类问题,最好使用约束满足启发式方法,这样可以更快地解决问题。 For more information I suggest that you read Constraint Satisfaction Problems chapter of Artificial Intelligence: A Modern Approach by Stuart Russel . 有关更多信息,建议您阅读Stuart Russel的“ Artificial Intelligence: A Modern ApproachConstraint Satisfaction Problems一章。 You can find the academic slides of this chapter online by searching it on the internet. 您可以通过在互联网上搜索找到本章的学术幻灯片。 Such as: 如:
http://www.ra.cs.uni-tuebingen.de/lehre/uebungen/ws13/AI/skript/AI_06_Constraint_Satisfaction_Problems_1-30.pdf 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