Solving a logic puzzle using Prolog

前端 未结 5 561
鱼传尺愫
鱼传尺愫 2020-12-13 16:27

The criminal is one of A, B, C and D.

A says: \"It\'s not me\"
B says: \"It\'s D\"
C says: \"It\'s B\"
D says: \"It\'s not me\"

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 16:48

    I ran across this problem and wanted to give it a shot :

    a(K) :- K \== a.
    b(d).
    c(b).
    d(K) :- K \== d.
    
    solve(TruthTeller) :-
        member(K, [a, b, c, d]),
        xor([a(K), b(K), c(K), d(K)], Truth),
        Truth =.. [TruthTeller|_].
    
    xor([Head|Tail], Result) :-
        (   call(Head)
         -> forall(member(X, Tail), \+ call(X)), Result = Head
         ;  xor(Tail, Result)).
    

提交回复
热议问题