它是传教士-食人族程序,在 legal(3,x).legal(0,x) 行发现错误。
% : mandc(state(3,3,left), [state(3,3,left)], Path)?
mandc(state(0, 0, right), _, []).
mandc(CurrentState, Visited, [Move | RestOfMoves]) :-
newstate(CurrentState, NextState),
not(member(NextState, Visited)),
make_move(CurrentState, NextState, Move),
mandc(NextState, [NextState | Visited], RestOfMoves).
% make_move(State1, State2, Move) builds the move(-,-,-) that gets you
% from State1 to State2.
make_move(state(M1, C1, left), state(M2, C2, right), move(M, C, right)) :-
M is M1 - M2,
C is C1 - C2.
make_move(state(M1, C1, right), state(M2, C2, left), move(M, C, left)) :- M is M2 - M1,
C is C2 - C1.
% carry(X,Y) is true if the boat can carry X missionaries and Y cannibals.
carry(2, 0).
carry(1, 0).
carry(1, 1).
carry(0, 1).
carry(0, 2).
% legal(X, Y) is true if it is safe for the missionaries to have X
% missionaries and Y cannibals together on a river bank (left or right).
legal(X, X).
**legal(3,X).
legal(0,X).**
% newstate(State1, State2)
newstate(state(M1, C1, left), state(M2, C2, right)) :-
carry(M, C),
M =< M1,
C =< C1,
M2 is M1 - M,
C2 is C1 - C,
legal(M2, C2).
newstate(state(M1, C1, right), state(M2, C2, left)) :-
carry(M, C),
M2 is M1 + M,
C2 is C1 + C,
M2 =< 3,
C2 =< 3,
legal(M2, C2).
任何人都可以说解决它吗?如果你有其他传教士计划 - 食人族然后给我。