Home‎ > ‎

Meeting Point SICStus

%% meeting.pl

% sicstus --goal "main(meetingins), halt." -l meeting

:- use_module(library(between)).

main(File) :-
    [File],
    board(Size),
    MaxX = Size, MaxY = Size,
    findall(number(X,Y,N),number(X,Y,N),Nums),
    between(1,MaxX,MX),
    between(1,MaxY,MY),
    \+ member(number(MX,MY,_),Nums),
    Visited = [],
    route(Nums,MX,MY,Visited,Plan),
    output_links(Plan).

route([number(X,Y,N)|Nums],MX,MY,Visited,Plan) :-
    \+ member((X,Y),Visited),    
    route(X,Y,X,Y,N,MX,MY,[(X,Y)|Visited],Visited1,Plan,Plan1),
    route(Nums,MX,MY,Visited1,Plan1).
route([],_MX,_MY,_Visited,Plan) :- Plan=[].
    
route(_PX,_PY,X,Y,N,X,Y,Visited,Visited1,Plan,PlanR) :- N=0, Plan=PlanR, Visited1=Visited.
route(PX,PY,X,Y,N,MX,MY,Visited,Visited1,Plan,PlanR) :-
    Xm1 is X-1,
    Xp1 is X+1,
    Ym1 is Y-1,
    Yp1 is Y+1,
    member((X1,Y1),[(X,Ym1),(X,Yp1),(Xm1,Y),(Xp1,Y)]),    
    cell(X1,Y1),
    \+ member((X1,Y1),Visited),
    Plan = [link(X,Y,X1,Y1)|Plan1],
    (X1\==PX, Y1\==PY ->
        N1 is N-1,
        N1 >= 0
    ;
        N1 = N
    ),
    ((X1,Y1) = (MX,MY) -> TmpVisited=Visited; TmpVisited = [(X1,Y1)|Visited]),
    route(X,Y,X1,Y1,N1,MX,MY,TmpVisited,Visited1,Plan1,PlanR).

cell(X,Y) :-
    board(N),
    X >= 1, X =< N,
    Y >= 1, Y =< N.
    
output_links([]).
output_links([Link|L]) :-
    portray_clause(Link),
    output_links(L).
Comments