簡體   English   中英

prolog列出了每個可能的遞歸路徑

[英]prolog list out every possible path of the recursion

我想列出所有可能的路徑

country(england,france). 
country(france,bulgaria).
country(bulgaria,germany).
country(england,bulgaria).
country(germany,italy).
edit: additional to

country(germany,italy).
country(england,italy).
country(england,greece).
country(greece,france).

connectto(X, Y) :-
country(X, Y).

?-OP(150,XFY,向)。

X到Y:-get_waypoints(X,Y,Waypoints),寫(Waypoints),失敗。

get_waypoints(Start, End, [Waypoint|Result]) :-
   country(Start, End),
   !;country(Start, Waypoint),
   get_waypoints(Waypoint, End, Result).

否則,系統會給出原始代碼

   | ?- england to italy.
   []no

從你提到的代碼。

現在問題出現了

    | ?- england to italy.
    [_31242|_31243][france,bulgaria,germany,_31332|_31333]   
    [bulgaria,germany,_31422|_31423]
    [greece,france,bulgaria,germany,_31602|_31603]no

雖然它顯示了所有可能的路線。

任何解決方案將不勝感激。

詢問您是否需要解釋:

country(bulgaria,germany).
country(england,bulgaria).
country(england,france). 
country(england,greece).
country(england,italy).
country(france,bulgaria).
country(greece,france).
country(germany,italy).

:- op(150, xfy, to).

X to Y :-
    findall(Waypoint, get_waypoints(X,Y,Waypoint), Waypoints),
    write(Waypoints).

get_waypoints(Start, End, []) :- 
    country(Start, End).
get_waypoints(Start, End, [Waypoint|Result]) :-
    country(Start, Waypoint),
    get_waypoints(Waypoint, End, Result).

用途是:

?- england to italy.

在這里,我更新了我的代碼以符合您的期望。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM