有没有prolog clpb编码可以解决这个涉及骑士、无赖和间谍的难题?

问题描述 投票:0回答:1

有三个人(Alex、Brook 和 Cody),其中一个是 骑士,一名恶棍,一名间谍。骑士总是告诉 事实是,无赖总是撒谎,而间谍要么撒谎,要么说谎 真相。 亚历克斯说:“科迪是个无赖。” 布鲁克说:“亚历克斯是 骑士。” 科迪说:“我是间谍。” 谁是骑士,谁是无赖,谁是间谍?通过 prolog clpb 解决这个难题

prolog clpb
1个回答
0
投票

可以解决这个逻辑难题:

:- use_module(library(clpb)).

knight_knave_spy(p(1, 0, 0), knight).
knight_knave_spy(p(0, 1, 0), knave).
knight_knave_spy(p(0, 0, 1), spy).

show_result(Name, P) :-
    write(Name),
    write(' is a '),
    knight_knave_spy(P, Type),
    writeln(Type).

% I means knight, A means knave, S means spy
says(p(I, A, S), X) :-
    % Knight truthful, Knave lies, Spy is either
    sat(((I * X) # (A * ~X)) # S).

cody :-
    % A person is one of a knight, knave or spy
    sat(card([1], [AI, AA, AS])),
    sat(card([1], [BI, BA, BS])),
    sat(card([1], [CI, CA, CS])),

    % Each person is a different type
    sat(card([1], [AI, BI, CI])),
    sat(card([1], [AA, BA, CA])),
    sat(card([1], [AA, BA, CA])),

    says(p(AI, AA, AS), CA),
    says(p(BI, BA, BS), AI),
    says(p(CI, CA, CS), CS),

    % Show answer
    show_result(alex, p(AI, AA, AS)),
    show_result(brook, p(BI, BA, BS)),
    show_result(cody, p(CI, CA, CS)).

swi-prolog 的结果:

?- cody.
alex is a knight
brook is a spy
cody is a knave
true ;
false.
© www.soinside.com 2019 - 2024. All rights reserved.