0
p(0,0).
p(0,1).
p(0,2).
p(0,3).
p(0,4).
p(1,1).
p(1,2).
p(1,3).
p(1,4).
p(1,0).
p(2,0).
p(2,1).
p(2,2).
p(2,3).
p(2,4).
p(3,0).
p(3,1).
p(3,2).
p(3,3).
p(3,4).
p(4,0).
p(4,1).
p(4,2).
p(4,3).
p(4,4).

adjacent(p(X,Y),p(X,Z)) :-
    p(X,Y),
    p(X,Z),
    Z is Y+1.
adjacent(p(X,Y),p(X,Z)) :-
    p(X,Y),
    p(X,Z),
    Z is Y-1.
adjacent(p(X,Y),p(Z,Y)) :-
    p(X,Y),
    p(X,Z),
    Z is X+1.
adjacent(p(X,Y),p(Z,Y)) :-
    p(X,Y),
    p(X,Z),
    Z is X-1.

adjacentC(X,Y) :-
    adjacent(X,Y).
adjacentC(X,Y) :-
    adjacent(X,Z),
    adjacentC(Z,Y).

I don't know why this code I wrote isn't working.

e.g.:

?- adjacentC((0,0),(4,4)). ERROR
false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

1

Quick answer: The following works and terminates always using closure/3 defined elsewhere.

adjacentD(X,Y) :-
   closure(adjacent,X,Y).

However, this approach is extremely slow, due to the inefficient definition of adjacent/3. Here is a better one / oh forget it, here is a more correct one, first:

adjacent2(p(X0,Y0),p(X,Y)) :-
   p(X0,Y0),
   (  X0 = X,
      p(X,Y),
      abs(Y0-Y) =:= 1
   ;  Y0 = Y,
      p(X,Y),
      abs(X0-X) =:= 1
   ).
Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209