:- use_module(library(lists),[member/2]). :- use_module(contestlib,[sublist/2, for/3]). mamadee(MazeSize,Entry,Exit,MaxWalls,Walls,Blocking) :- for(I,1,MaxWalls), length(Blocking,I), sublist(Walls,Blocking), Been = [Entry], \+ exists_path(Entry,Exit,MazeSize,Been,Blocking), !. exists_path(Entry,Exit,MazeSize,Been,Blocking) :- ( Exit == Entry -> true ; step(Entry,NewEntry,MazeSize,Blocking), \+ member(NewEntry,Been), exists_path(NewEntry,Exit,MazeSize,[NewEntry|Been],Blocking) ). step((A,B),(X,Y),MazeSize,Blocking) :- ( X = A, Y is B + 1, C is A - 1, D = B, U = A, V = B ; X = A, Y is B - 1, C is X - 1, D = Y, U = X, V = Y ; Y = B, X is A + 1, C = A, D is B - 1, U = A, V = B ; Y = B, X is A - 1, C = X, D is Y - 1, U = X, V = Y ), 1 =< X, X =< MazeSize, 1 =< Y, Y =< MazeSize, \+ member(wall((C,D),(U,V)),Blocking), \+ member(wall((U,V),(C,D)),Blocking).