1
我已經寫在序言的路徑:如何處理在序言圖的遍歷
edge(x, y).
edge(y, t).
edge(t, z).
edge(y, z).
edge(x, z).
edge(z, x).
path(Start, End, Path) :-
path3(Start, End, [Start], Path).
path3(End, End, RPath, Path) :-
reverse(RPath, Path).
path3(A,B,Path,[B|Path]) :-
edge(A,B),
!.
path3(A, B, Done, Path) :-
edge(A, Next),
\+ memberchk(Next, Done),
path3(Next, B, [Next|Done], Path).
其照顧循環圖,以及,我得到一個不規則的輸出,當我嘗試遍歷從同一個節點相同的節點。
如:path(x,x,P).
預期的輸出應該是:
P = [x, z, t, y, x]
P = [x, z, y, x]
P = [x, z, x]
但是,我得到的輸出:
p = [x] ------------> wrong case
P = [x, z, t, y, x]
P = [x, z, y, x]
P = [x, z, x]
我怎樣才能擺脫這種有害的情況。 感謝
程序中的截圖不正確。要看到這個,試試'path(x,Y,P)',其中'Y'是一個變量。 – false 2014-12-03 13:32:22