1

我已经编写了这段代码,但是当我在 GNU Prolog 中查阅它时,它显示如下。

在此处输入图像描述

这是我的代码

initial(left,3,3,0,0).
final(right,0,0,3,3).
P1=3000.
P2=5000.
P3=8000.
A is P1.
B is P2.
C is P3.

bad([_,A1,B1,C1,A2,B2,C2]):-
   (   A1>B1,B1>C1,A1>C1
   ;   A2>B2,B2>C2,A2>C2
   ).

start:-
   initial(S),
   rez(S,Path),
   my_write(Path).

rez(State,Sol):-
   bkt(State,[],Sol).

bkt(State,Path,[State|Path]):-
   final(State).
bkt(State,Path,Sol) :-
   arc(State,N1),
   \+bad(N1),
   \+member(N1,Path),
   bkt(N1, [State|Path],Sol).

arc([left,As,Bs,Cs,Ad,Bd,Cd], [right,As1,Bs1,Cs1,Ad1,Bd1,Cd1]) :-
   boat(A,B,C),
   As >= A, As1 is As-A,
   Bs >= B, Bs1 is Bs-B,
   Cs >= C, Cs1 is Cs-C,
   Ad1 is Ad+A,
   Bd1 is Bd+B,
   Cd1 is Cd+C.
arc([right,As,Bs,Cs,Ad,Bd,Cd], [left,As1,Bs1,Cs1,Ad1,Bd1,Cd1]):-
   boat(A,B,C),
   Ad >= A, Ad1 is Ad-A,
   Bd >= B, Bd1 is Bd-B,
   Cd >= C, Cd1 is Cd-C,
   As1 is As+A,Bs1 is Bs+B, Cs1 is Cs+C.

boat(A,B,C):-
   member([A,B,C],[[0,1],[1,0],[1,1],[2,0],[0,2]]).

my_write([]).
my_write([H|T]):-
   write(H),
   nl,
   my_write(T).

我希望大家能帮助我修复这段代码,以便在我在 GNU Prolog 中咨询时它可以运行良好。

4

0 回答 0