Recently the Guardian posted a "Vietnam snake" math puzzle:
The puzzle asks to fill empty cells with integer numbers 1..9, using every number only once, such that the equality holds(order of operations: multiply and divide first, then add and subtract; colon : represents division).
Here is my program to solve the puzzle usingProlog-based ECLiPSe constraint logic programming system(https://github.com/kit1980/sdymchenko-com/blob/master/snake-puzzle-eclipse/snake.ecl):
:- lib(ic). puzzle(A + 13 * B / C + D + 12 * E - F - 11 + G * H / I - 10 #= 66). snake(Puzzle) :- term_variables(Puzzle, Vars), [A,B,C,D,E,F,G,H,I] = Vars, Vars :: 1..9, alldifferent(Vars), call(Puzzle), labeling(Vars). main :- puzzle(Puzzle), findall(_, ( snake(Puzzle), writeln(Puzzle) ), _).
The first line imports interval arithmetic constraint programming library ic.The puzzle predicate defines the linearized version of the snake puzzle from the picture.
snake constrains all variables in Puzzle to be integers in 1..9 range and pairwise different, forces the Puzzle constraint (using call),and finds a possible concrete assignment of the variables that satisfy all constraints (using labeling).
main assigns the definition of the puzzle to the Puzzle variable (it's not hard to modify this to input the puzzle definition from the user at runtime),and then uses Prolog's findall to find and print every solution.
Note that constraint programming is not just brute-force approach: clever algorithms are used behind the scenes to prune the search space.
This version assumes that every intermediate step should result in an integer (# in #= means integrality constraint on the intermediate values).After running the program we get 20 solutions:
6 + 13 * 9 / 3 + 5 + 12 * 2 - 1 - 11 + 8 * 7 / 4 - 10 #= 66 5 + 13 * 9 / 3 + 6 + 12 * 2 - 1 - 11 + 8 * 7 / 4 - 10 #= 66 9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 8 * 7 / 4 - 10 #= 66 6 + 13 * 3 / 1 + 9 + 12 * 2 - 5 - 11 + 8 * 7 / 4 - 10 #= 66 6 + 13 * 9 / 3 + 5 + 12 * 2 - 1 - 11 + 7 * 8 / 4 - 10 #= 66 5 + 13 * 9 / 3 + 6 + 12 * 2 - 1 - 11 + 7 * 8 / 4 - 10 #= 66 9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 7 * 8 / 4 - 10 #= 66 6 + 13 * 3 / 1 + 9 + 12 * 2 - 5 - 11 + 7 * 8 / 4 - 10 #= 66 7 + 13 * 3 / 1 + 5 + 12 * 2 - 6 - 11 + 9 * 8 / 4 - 10 #= 66 5 + 13 * 3 / 1 + 7 + 12 * 2 - 6 - 11 + 9 * 8 / 4 - 10 #= 66 7 + 13 * 3 / 1 + 5 + 12 * 2 - 6 - 11 + 8 * 9 / 4 - 10 #= 66 5 + 13 * 3 / 1 + 7 + 12 * 2 - 6 - 11 + 8 * 9 / 4 - 10 #= 66 9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 8 * 3 / 6 - 10 #= 66 5 + 13 * 4 / 1 + 9 + 12 * 2 - 7 - 11 + 8 * 3 / 6 - 10 #= 66 9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 3 * 8 / 6 - 10 #= 66 5 + 13 * 4 / 1 + 9 + 12 * 2 - 7 - 11 + 3 * 8 / 6 - 10 #= 66 5 + 13 * 2 / 1 + 3 + 12 * 4 - 7 - 11 + 9 * 8 / 6 - 10 #= 66 3 + 13 * 2 / 1 + 5 + 12 * 4 - 7 - 11 + 9 * 8 / 6 - 10 #= 66 5 + 13 * 2 / 1 + 3 + 12 * 4 - 7 - 11 + 8 * 9 / 6 - 10 #= 66 3 + 13 * 2 / 1 + 5 + 12 * 4 - 7 - 11 + 8 * 9 / 6 - 10 #= 66
If we change #= to $= in the puzzle definition, intermediate steps will be no longer constrained to be integers, and we'll get 136 solutions.
To learn more about ECLiPSe and get pointers to relevant resources see eclipse-clp tag on this blog.
使用ECLiPSe解决越南蛇数学谜题
本文介绍了一种使用基于ECLiPSe约束逻辑编程系统的Prolog程序来解决越南蛇数学谜题的方法。通过将谜题转换为约束条件,并利用Prolog的搜索和求解能力,该程序能够找到满足所有约束条件的解决方案。最终,程序输出了20个符合要求的解,展示了ECLiPSe在复杂问题求解方面的强大能力。
4295

被折叠的 条评论
为什么被折叠?



