[英]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 Approach
的Constraint 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.