0

它是传教士-食人族程序,在 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).

任何人都可以说解决它吗?如果你有其他传教士计划 - 食人族然后给我。

4

1 回答 1

1

问题是所讨论的两个事实每个都只有一个变量 X 的外观(因此,它们是“单例”)。如果您不需要表示两个(或更多)事物需要匹配,而只是表示它可以是任何东西,请使用下划线。虽然这不应该是一个错误,而是一个警告。

于 2012-05-23T01:37:46.377 回答