Prolog - 尝试解决文本难题
Posted
技术标签:
【中文标题】Prolog - 尝试解决文本难题【英文标题】:Prolog - trying to solve textual puzzle 【发布时间】:2013-08-19 19:42:38 【问题描述】:我目前正在尝试学习一点 Prolog。作为一个练习,我试图解决以下谜题:
给出这些规则:
*Every person that has neither a car nor a plane, has a bike.
*Every person that doesn't have a plane but has a bike, has a car
*Every person that doesn't have a plane but has a car, has a truck
*Every person that doesn't have a truck but has a boat, doesn't have a plane
*Every person that doesn't have a boat but has a plane, doesn't have a car
现在有四个人:
*Person1 doesn't have a car but has a boat
*Person2 doesn't have a boat but has a plane
*Person3 doesn't have a plane but has a bike
*Person4 doesn't have a bike but has a car
谁没有卡车?
到目前为止,我想出的是:
doesnthave(car,pa).
has(boat,pa).
doesnthave(boat,pb).
has(plane,pb).
doesnthave(plane,pc).
has(bike,pc).
doesnthave(bike,pd).
has(car,pd).
has(bike,X) :- doesnthave(car,X),doesnthave(plane,X).
has(car,X) :- doesnthave(plane,X),has(bike,X).
has(truck,X) :- doesnthave(plane,X),has(car,X).
doesnthave(plane,X) :- doesnthave(truck,X),has(boat,X).
doesnthave(car,X) :- doesnthave(boat,X),has(plane,X).
现在这似乎还不够。或者这不是prolog中解决此类谜题的方法吗?
编辑:前两个陈述似乎是矛盾的。它们共同产生:每个既没有汽车也没有飞机的人都有汽车。我不确定是否有合理的解决方案。
【问题讨论】:
【参考方案1】:不确定这个解决方案,但更简单的知识表示肯定会有所帮助:
has(car, pa, n). has(boat, pa, y).
has(boat, pb, n). has(plane, pb, y).
has(plane, pc, n). has(bike, pc, y).
has(bike, pd, n). has(car, pd, y).
has(bike, X, y) :- has(car, X, n), has(plane, X, n).
has(car, X, y) :- has(plane, X, n), has(bike, X, y).
has(truck, X, y) :- has(plane, X, n), has(car, X, y).
has(plane, X, n) :- has(truck, X, n), has(boat, X, y).
has(car, X, n) :- has(boat, X, n), has(plane, X, y).
现在我们可以查询所有权是什么(注意这是一对多的关系)
?- setof((P,T,R), has(T,P,R), L), maplist(writeln, L).
pa,boat,y
pa,car,n
pb,boat,n
pb,car,n
pb,plane,y
pc,bike,y
pc,car,y
pc,plane,n
pc,truck,y
pd,bike,n
pd,car,y
L = [ (pa, boat, y), (pa, car, n), (pb, boat, n), (pb, car, n), (pb, plane, y), (pc, bike, y), (pc, car, y), (pc, ..., ...), (..., ...)|...].
注意,我将P
erson 放在T
ransport 之前,然后我们可以直观地检查结果...
看来解决办法是三重...
【讨论】:
事实证明,前两个陈述存在矛盾。它们共同产生:每个既没有汽车也没有飞机的人都有汽车。以上是关于Prolog - 尝试解决文本难题的主要内容,如果未能解决你的问题,请参考以下文章
在 CLPQ/R (Prolog) 中解决一个简单的几何难题