%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%pocible moves
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%papa and mama moved
move((papa(X),mama(X),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),((papa(X_N),mama(X_N),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N)))):-
        otherBank(X,X_N),
        %otherBank(M,M_N),
        otherBank(B,B_N).
%papa and policeman moved
move((papa(X),mama(M),policeman(X),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(X_N),mama(M),policeman(X_N),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        %otherBank(Pl,Pl_N),
        otherBank(B,B_N).
%papa and son1 moved
move((papa(X),mama(M),policeman(Pl),prisoner(Pr),son1(X),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(X_N),mama(M),policeman(Pl),prisoner(Pr),son1(X_N),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        %otherBank(S1,S1_N),
        otherBank(B,B_N).
%papa and son2 moved
move((papa(X),mama(M),policeman(Pl),prisoner(Pr),son1(S1),son2(X),daughter1(D1),daughter2(D2),boat(B)),(papa(X_N),mama(M),policeman(Pl),prisoner(Pr),son1(S1),son2(X_N),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        %otherBank(S2,S2_N),
        otherBank(B,B_N).
%papa moved lonely
move((papa(X),mama(M),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(X_N),mama(M),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%mama and policeman moved
move((papa(P),mama(X),policeman(X),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(X_N),policeman(X_N),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%mama and daughter1 moved
move((papa(P),mama(X),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(X),daughter2(D2),boat(B)),(papa(P),mama(X_N),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(X_N),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%mama and daughter2 moved
move((papa(P),mama(X),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(X),boat(B)),(papa(P),mama(X_N),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(X_N),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%mama moved lonely
move((papa(P),mama(X),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(X_N),policeman(Pl),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman and prisoner moved
move((papa(P),mama(M),policeman(X),prisoner(X),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(X_N),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman and son1 moved
move((papa(P),mama(M),policeman(X),prisoner(Pr),son1(X),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(Pr),son1(X_N),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman and son2 moved
move((papa(P),mama(M),policeman(X),prisoner(Pr),son1(S1),son2(X),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(Pr),son1(S1),son2(X_N),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman and daughter1 moved
move((papa(P),mama(M),policeman(X),prisoner(Pr),son1(S1),son2(S2),daughter1(X),daughter2(D2),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(Pr),son1(S1),son2(S2),daughter1(X_N),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman and daughter2 moved
move((papa(P),mama(M),policeman(X),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(X),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(X_N),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%policeman moved lonely
move((papa(P),mama(M),policeman(X),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B)),(papa(P),mama(M),policeman(X_N),prisoner(Pr),son1(S1),son2(S2),daughter1(D1),daughter2(D2),boat(B_N))):-
        otherBank(X,X_N),
        otherBank(B,B_N).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Equality check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
areEqual(A,A).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Restrictions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
restricted(State):-
        %policeman and prisoner are on different banks
        areEqual(State,(_,_,A,B,_,_,_,_,_)),
        otherBank(A,B);
        %papa face to face with one of the daughters
        areEqual(State,(A,_,_,_,_,_,A,_,_));
        areEqual(State,(A,_,_,_,_,_,_,A,_));
        %mama face to face with one of the sons
        areEqual(State,(_,A,_,_,A,_,_,_,_));
        areEqual(State,(_,A,_,_,_,A,_,_,_)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Find path
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bigMove(Finish,Finish,Path,_,Result):-
        append([Finish],Path,Result1),
        reverse(Result1,Result),
        !.
bigMove(Start,Finish,Path,Visited,Result):-
        move(Start,Step),
        not(restricted(Step)),
        not(member(Step,Visited)),!,
        append([Step],Visited,NewVisited),
        append([Start],Path,Path1),
        bm(Step,Finish,Path1,NewVisited,Result).
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
papa(left).
papa(right).
mama(left).
mama(right).
policeman(left).
policeman(right).
prisoner(left).
prisoner(right).                
son1(left).
son1(right).    
son2(left).
son2(right).    
daughter1(left).
daughter1(right).       
daughter2(left).
daughter2(right).       
boat(left).
boat(right).    
smth(papa(X),mama(X),X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
otherBank(right,left).
otherBank(left,right).