s72 Once upon a time a farmer went to market and purchased a fox, a goose, and a
ID: 3775085 • Letter: S
Question
s72
Once upon a time a farmer went to market and purchased a fox, a goose, and a bag of beans. On his way home, the farmer came to the bank of a river and hired a boat. But in crossing the river by boat, the farmer could carry only himself and a single one of his purchases - the fox, the goose, or the bag of the beans. If left alone, the fox would eat the goose, and the goose would eat the beans. The farmer's challenge was to carry himself and his purchases to the far bank of the river, leaving each purchase intact. How did he do it? Solve the problem above using PROLOG with BFS %% Test. test:- go(state(0, 0, 8), state(0, 4, 4)). %% --- % move is an edge in state space. Build the state space. %large rightarrow small move(state(S, M, L), state(3, M, NL)):- S 3, NL is L - (3 - S). move(state (S, M, L), state (NS, M, 0)):- SExplanation / Answer
let us define the state of the puzzle at any time:
state([Farmer, Fox, Goose, Beans])
The initial and final state are:
initial(state([left, left, left, left])).
final(state([right, right, right, right]))
The various transition rules are:
to_cross(state([left,X,Y,Z]),state([right,X,Y,Z]), farmer_crosses_river).
to_cross(state([right,X,Y,Z]),state([left,X,Y,Z]), farmer_comes_back).
to_cross(state([left,X,left,Z]),state([right,X,right,Z]), bring_goose_over).
to_cross(state([right,X,right,Z]),state([left,X,left,Z]), bring_goose_back).
to_cross(state([left, left, X, Y]),state([right, right, X, Y]), bring_fox_over).
to_cross(state([right, right, X, Y]),state([left, left, X, Y]), bring_fox_back).
to_cross(state([left, X, Y, left]),state([right, X, Y, right]), bring_beans_over).
to_cross(state([right, X, Y, right]),state([left, X, Y, left]), bring_beans_back)
To check state is legal:
check_allowed(st([X, Y, Y, _])) :- X == Y.
check_not_allowed(st([X, _, Y, Y])) :- X == Y.
river_aux(A,A,_,[]). % To go from A to A the crossing is empty meaning that we do not cross at all, this will be true when we reach our destination.
river_aux(A,B,V,P) :-
to_cross(A,C,Action),
not(check_not_allowed(C)),
not(member(C,V)),
river_aux(C,B,[C|V],Plan),
P = [Action | Plan].
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.