我需要一个谓词路由,它给出开始和结束之间的所有城市。例如:
path(chicago,atlanta).
path(chicago,milwaukee).
path(milwaukee,detroit).
path(milwaukee,newyork).
path(chicago,detroit).
path(detroit, newyork).
path(newyork, boston).
path(atlanta,boston).
path(atlanta, milwaukee).
?- routing(chicago,newyork,X).
X=[chicago,milwaukee,newyork];
X=[chicago,detroit,newyork];
X=[chicago,milwaukee,detroit,newyork];
X=[chicago,atlanta,milwaukee,newyork];
X=[chicago,atlanta,milwaukee,detroit,newyork]
我已经尝试过这个,并继续回来。
routing(FromCity,ToCity,[FromCity|ToCity]) :-
path(FromCity,ToCity).
routing(FromCity,ToCity,[FromCity|Connections]) :-
path(FromCity,FromConnection),
path(FromConnection,ToConnection),
path(ToConnection,ToCity),
routing(ToConnection,ToCity,Connections).
routing(FromCity,ToCity,[]).
但它只是不断给予
X=[chicago,milwaukee,newyork];
X=[chicago,chicago,newyork];
X=[chicago,chicago,chicago,newyork]
...
..
有人可以指出我正确的方向吗...