We can carry a count with us as we move, this works for movesto(a,e, Dist) but crashes my GNU-Prolog for impossible case movesto(c,e), I do not know what should be changed:
/*--------------
connections between points
--------------*/
link(a,b).
link(b,c).
link(b,d).
link(d,e).
/*--------------
logic carried out to connect the movements.
--------------*/
movesto(A,B,1):- link(A,B).
movesto(A,B,2):- link(A,C), link(C,B).
movesto(A,B,Dist):- movesto(A,C,Dist1), movesto(C,B,Dist2), Dist is Dist1 + Dist2.
pyTony
pyMod
6,305 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
The first statement must be link to reduce search:
/*--------------
connections between points
--------------*/
link(a,b).
link(b,c).
link(b,d).
link(d,e). link(c,f). link(f,h).
/*--------------
logic carried out to connect the movements.
--------------*/
movesto(A,B,1):- link(A,B).
movesto(A,B,Dist):- link(A,C), movesto(C,B,Dist1), Dist is Dist1 + 1.
| ?- movesto(b,X,D).
D = 1
X = c ? ;
D = 1
X = d ? ;
D = 2
X = f ? ;
D = 3
X = h ? ;
D = 2
X = e ? ;
(31 ms) no
| ?-
pyTony
pyMod
6,305 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
Question Answered as of 1 Year Ago by
pyTony