Labyrinth problem

From: Negator (negator_at_comtv.ru)
Date: 04/11/04


Date: Sun, 11 Apr 2004 18:01:17 +0400

The task is to find all ways from Start to Destination in a labirinth. The
labyrinth is defined as the quantity of facts as follows:

link(room1,room2).
link(room2,room3).
...
link(roomK,room1).
...
link(roomN,roomK).

where roomK are room numbers and link means that these rooms are connected.
The way can be layed in both directions (i.e. from room1 to room2 and from
room2 to room1).

The following predicate throws "out of stack" error because of recursion
caused by marked lines.

path(A,A,L):-length(L,1).
path(From,To,A):-A=[From|Rest],
 Rest=[SecondInPair|_],
(pass(SecondInPair,From); /* recursion*/
 pass(From,SecondInPair)),
 path(SecondInPair,To,Rest).

The recursion looks like this:

?-path(room1,room2).

[room1,room2,room1,room2...]

How to get rid of recursion in this predicate?

Thanks in advance.