使用 Prolog 解决逻辑难题 [英] Solving a logic puzzle using Prolog

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

问题描述

罪犯是 A、B、C 和 D 之一.

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

A 说:不是我"
B 说:是 D"
C 说:这是 B"
D 说:这不是我"

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

而且我们知道只有其中一个说的是实话.

And we know that only one of them tells the truth.

是谁?我想用Prolog来解决.

Who is the one? I want to solve it by using Prolog.

这是一道面试题.

推荐答案

单线解决方案

?- member(K,[a,b,c,d]),(K\=a->A=1;A=0),(K=d->B=1;B=0),(K=b->C=1;C=0),(K\=d->D=1;D=0),A+B+C+D=:=1.
K = a,
A = 0,
B = 0,
C = 0,
D = 1 ;
false.

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

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