Prolog Domino解决方案 [英] Prolog Domino Solution

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

问题描述

我需要一种算法,该算法给出一组多米诺骨牌,将所有可能的结局归还游戏.

I need an algorithm that given a set of domino pieces, returns every possible end to the game.

我已经找到了这个游戏, Prolog多米诺骨牌游戏,但它只是在开始阶段增加了一些内容集合,所以它不能为您提供所有可能的解决方案.

I have already found this one, Prolog domino game, but it only adds pieces to the beggining of the set, so it doesn't give you every possible solution.

我用这个 [[5,4],[4,3],[3,2],[2,1]] ,并尝试添加此行 domino_order(In,X,[Out | [X,Y]]):: select(Piece,In,Remaining),swap_or_not(Piece,[X,Y]),domino_order(Remaining,Y,Out).,但它不起作用.

I replaced this [5-4, 4-3, 3-2, 2-1], with this [[5,4], [4,3], [3,2], [2,1]], and tried adding this line domino_order(In, X, [Out|[X,Y]]) :- select(Piece, In, Remaining), swap_or_not(Piece, [X,Y]), domino_order(Remaining, Y, Out)., but it doesn't work.

推荐答案

写下详细的逻辑将导致代码变得有些复杂.我建议改为快速检查其有效性,然后让Prolog算出插入点.

writing down the detailed logic would lead to somewhat complex code. I suggest instead to have a quick check for validity, and let Prolog work out the insertion points.

domino :-
    Spare = [4-7,3-4], Curr = [1-2,2-3],
    domino_row_add_spare(Curr, Spare, R),
    writeln(R).

domino_row_add_spare(C, [], C).
domino_row_add_spare(C, Sps, U) :-
    append(L, R, C),
    select(X-Y, Sps, Rest),
    (append(L, [X-Y|R], C1) ; append(L, [Y-X|R], C1)),
    valid(C1),
    domino_row_add_spare(C1, Rest, U).

valid([_]).
valid([_-X,X-Y|R]) :- valid([X-Y|R]).

这篇关于Prolog Domino解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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