[英]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.