本文介绍了“建筑物"指的是“建筑物".序言之谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决Prolog中的一个难题.谜语是:有两栋建筑物,每栋都有树型公寓(每层公寓):一栋3房的公寓,一栋4房的公寓,一栋5房的公寓.

I'm trying to solve a riddle in Prolog. the riddle is:There are two buildings, each one has tree apartments (apartment per floor): one apartment of 3 rooms,one of 4 rooms and one of 5 rooms.

Dana,Joni和Noah住在1号楼.罗恩,盖尔和阿伦住在2号楼.

Dana,Joni and Noah lives in building 1.Ron,Gale and Aron lives in building 2.

Joni的公寓比Dana和Noah高.这意味着他住在1号楼的三楼.诺亚(Noah)和大风(Gale)生活在同一楼层(位于不同的建筑物中).罗恩的房间比阿伦多一间.罗恩住在大风之上一层.2号楼最高的公寓是5个房间的公寓.

Joni apartment is higher then Dana and Noah. It means he lives on the third floor of building 1.Noah and Gale lives on the same floor (in different buildings).Ron has a one more room than Aron.Ron lives one floor above Gale.The highest apartment in building 2 is the 5 rooms apartment.

我需要找到每个人都住在哪个楼层.

I need to find in which floor everyone lives.

我写了这段代码:

solve([dana,building1,F1,R1],[noah,building1,F2,R2],[joni,building1,F3,R3],[gale,building2,F4,R4],[ron,building2,F5,R5],[aron,building2,F6,R6]
      ) :-
   L =[[dana,building1,1,3],[dana,building1,1,4],[dana,building1,1,5],[dana,building1,2,3],[dana,building1,2,4],[dana,building1,2,5],[dana,building1,3,3],[dana,building1,3,4],[dana,building1,3,5]
,[noah,building1,1,3],[noah,building1,1,4],[noah,building1,1,5],[noah,building1,2,3],[noah,building1,2,4],[noah,building1,2,5],[noah,building1,3,3],[noah,building1,3,4],[noah,building1,3,5]
,[joni,building1,1,3],[joni,building1,1,4],[joni,building1,1,5],[joni,building1,2,3],[joni,building1,2,4],[joni,building1,2,5],[joni,building1,3,3],[joni,building1,3,4],[joni,building1,3,5]
,[gale,building1,1,3],[gale,building1,1,4],[gale,building1,1,5],[gale,building1,2,3],[gale,building1,2,4],[gale,building1,2,5],[gale,building1,3,3],[gale,building1,3,4],[gale,building1,3,5]
,[ron,building1,1,3],[ron,building1,1,4],[ron,building1,1,5],[ron,building1,2,3],[ron,building1,2,4],[ron,building1,2,5],[ron,building1,3,3],[ron,building1,3,4],[ron,building1,3,5]
,[aron,building1,1,3],[aron,building1,1,4],[aron,building1,1,5],[aron,building1,2,3],[aron,building1,2,4],[aron,building1,2,5],[aron,building1,3,3],[aron,building1,3,4],[aron,building1,3,5]],
   F3 > F2,
   F3>F1,
   F2 == F4,
   R5 == R6-1,
   F5 == F4+1,
   (F4 == 3, R4 == 5;F5 == 3, R5 == 5; F6 == 3, R6 == 5),
   del([dana,building1,F1,R1],L,List1),
   del([noah,building1,F2,R2],List1,List2),
   del([joni,building1,F3,R3],List2,List3),
   del([gale,building2,F4,R4],List3,List4),
   del([ron,building2,F5,R5],List4,List5),
   del([aron,building2,F6,R6],List5,_).

del(X,L,L1) :-
   remove(X,L,L1).

但是当我执行查询时:

solve([dana,building1,F1,R1],[noah,building1,F2,R2],[joni,building1,F3,R3],[gale,building2,F4,R4],[ron,building2,F5,R5],[aron,building2,F6,R6]).

我得到:

"Error 22 : Instantiation Error"

有人吗?我不明白我做错了什么.

anyone?I don't understand what I do wrong.

推荐答案

我终于做到了.这就是答案:

I finally did it.This is the answer:

solve([dana,building1,F1,R1],
      [noah,building1,F2,R2],
      [joni,building1,F3,R3],
      [gale,building2,F4,R4],
      [ron,building2,F5,R5],
      [aron,building2,F6,R6]) :-
   assign([1,2,3],[F1,F2,F3]),
   assign([1,2,3],[F4,F5,F6]),
   assign([3,4,5],[R1,R2,R3]),
   assign([3,4,5],[R4,R5,R6]),
   F3 > F2,
   F3>F1,
   F2 =:= F4,
   R5 =:= R6-1,
   F5 =:= F4+1,
   (  F4 =:= 3, R4 =:= 5
   ;  F5 =:= 3, R5 =:= 5
   ;  F6 =:= 3, R6 =:= 5
   ).

assign(_,[]).
assign(Digs,[D|Vars]):-
   del(D,Digs,Digs1),
   assign(Digs1,Vars).

del(X,L,L1):-
   remove(X,L,L1).

这篇关于“建筑物"指的是“建筑物".序言之谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:02