我在Prolog中編寫的一個程序與查找從起始位置到結束位置的所有可能路徑選項有關。Prolog - 查找所有路徑選項
這是我到目前爲止有:
findAllRoutes(Start, End, Path) :-
findAllRoutes(Start, _, End, Path),
print('Successful route: '), print(Route).
findAllRoutes(End, _, End, [End]). %route was successfully finished
findAllRoutes(Start, Temp, End, [Start|Rest_of_List]) :-
path(Start, Temp),
findAllRoutes(Temp, _, End, Rest).
這裏是要讀取的數據:
%path(Start, End).
path(1, 4). %find all the possible paths from location 1 to location 4.
%paths_in_place[[Start, End, Distance]].
paths_in_place[[1, 2, 250], [2, 4, 250], [1, 3, 400], [3, 4, 300]].
我的問題是,這是一個正確的方式來循環paths_in_place
同時還保存從起始位置到結束位置途中到達的點的順序?
另外,Distance
發生什麼情況時findAllRoutes
被稱爲沒有提及Distance
字段?在Prolog中合法傳遞Start, End, Route
的參數是否合法,即使在paths_in_place
的字段是Start, End, Distance
?
任何幫助,非常感謝。讓我知道是否需要澄清任何事情。