序言模糊输出 [英] Prolog Ambiguous Output

查看:48
本文介绍了序言模糊输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Prolog 编写一个迷你数独求解器.数独由 4x4 字段组成,例如

I'm writing a mini sudoku solver in Prolog. The sudoku consists of 4x4 fields, e.g.

_ _ 2 3
_ _ _ _
_ _ _ _
3 4 _ _

如果数独只有一种可能的解决方案(如上面的数独),我的程序将按预期运行.但是如果我用一个空白替换至少一个给定的字段(例如用_替换 4),Prolog 的输出是这样的:

If the sudoku is has only one possible solution (as the sudoku above), my program works as expected. But if I replace at least one given field with a gap (e.g. by replacing 4 by _), the output of Prolog is like this:

[_#3(1:4),_#23(1:4),2,3.....

我猜_#3(1:4)"的意思是取 1 或 4",但我希望每个解决方案都在一个单独的、无歧义的列表中.这是我的代码:

I guess "_#3(1:4)" means "take 1 or 4", but I want each solution in a separated, non-ambiguous list. This is my code:

sudoku(Puzzle,Solution):-
        Puzzle=Solution,
        fd_domain(Solution,1,4),

        Puzzle=[S11,S12,S13,S14,
            S21,S22,S23,S24,
            S31,S32,S33,S34,
            S41,S42,S43,S44],

        fd_all_different([S11,S12,S13,S14]),
        fd_all_different([S21,S22,S23,S24]),
        fd_all_different([S31,S32,S33,S34]),
        fd_all_different([S41,S42,S43,S44]),

        fd_all_different([S11,S21,S31,S41]),
        fd_all_different([S12,S22,S32,S42]),
        fd_all_different([S13,S23,S33,S43]),
        fd_all_different([S14,S24,S34,S44]),

        fd_all_different([S11,S12,S21,S22]),
        fd_all_different([S13,S14,S23,S24]),
        fd_all_different([S31,S32,S41,S42]),
        fd_all_different([S33,S34,S43,S44]).

当我将它加载到 Gnu Prolog 中时,我输入了以下查询:

When I loaded it into Gnu Prolog, I type the following query:

sudoku([_,_,2,3,_,_,_,_,_,_,_,_,3,_,_,_],Sol).

我做错了什么?

推荐答案

您的 Prolog 为您提供了多种解决方案的建议,显示了存储中的 残余 约束.您需要标记变量,即让引擎枚举有效值.

your Prolog is advising you of multiple solution, showing the residual constraints in store. You need to label the variables, i.e. let the engine enumerate valid values.

?- sudoku([_,_,2,3,_,_,_,_,_,_,_,_,3,_,_,_],Sol),fd_labeling(Sol).

Sol = [1,4,2,3,2,3,1,4,4,1,3,2,3,2,4,1] ? ;

Sol = [1,4,2,3,2,3,1,4,4,2,3,1,3,1,4,2] ? ;

Sol = [1,4,2,3,2,3,4,1,4,1,3,2,3,2,1,4] ? ;

Sol = [4,1,2,3,2,3,1,4,1,4,3,2,3,2,4,1] ? ;

Sol = [4,1,2,3,2,3,4,1,1,2,3,4,3,4,1,2] ? ;

Sol = [4,1,2,3,2,3,4,1,1,4,3,2,3,2,1,4]

这篇关于序言模糊输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆