Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
%% Від r -> l travel(r,l). %% Від l -> r travel(l,r). %% Можливі варіанти. move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :- travel(X,Y). move([X,Wolf,X,Cabbage],goat,[Y,Wolf,Y,Cabbage]) :- travel(X,Y). move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :- travel(X,Y). move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]) :- travel(X,Y). %% Безпечні умови. safe([X,_,X,_]). % Коза на тому ж березі, що й фермер. safe([X,X,_,X]). % Вовк і капуста на одному березі з фермером solve([r,r,r,r],[]). solve(State,[FirstMove|OtherMoves]) :- move(State, FirstMove, NextState), safe(NextState), solve(NextState, OtherMoves). % ?- length(X,7), solve([l,l,l,l],X). % X = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; % X = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; % X = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; % X = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; % false. % ?- length(X,7), setof(t,solve([l,l,l,l],X),_). % X = [goat, nothing, cabbage, goat, wolf, nothing, goat] % X = [goat, nothing, wolf, goat, cabbage, nothing, goat] % % not_member(_, []) :- !. not_member(X, [Head|Tail]) :- X \= Head, not_member(X, Tail). solve2(AnsPath) :- solve2([[l,l,l,l]], AnsPath). solve2([State|_], [State]) :- State = [r,r,r,r], !. solve2([State|Path], [State|AnsPath]) :- move(State, _, NextState), safe(NextState), not_member(NextState,[State|Path]), solve2([NextState, State| Path], AnsPath). % ?- solve2(X). % X = [[w, w, w, w], [e, w, e, w], [w, w, e, w], [e, e, e, w], [w, e, w, w], [e, e, w, e], [w, e, w, e], [e, e, e, e]] ; % X = [[w, w, w, w], [e, w, e, w], [w, w, e, w], [e, e, e, w], [w, e, w, w], [e, e, w, e], [w, e, w, e], [e, e, e, e]] ; % X = [[w, w, w, w], [e, w, e, w], [w, w, e, w], [e, w, e, e], [w, w, w, e], [e, e, w, e], [w, e, w, e], [e, e, e, e]] ; % X = [[w, w, w, w], [e, w, e, w], [w, w, e, w], [e, w, e, e], [w, w, w, e], [e, e, w, e], [w, e, w, e], [e, e, e, e]] ;