在2点之间进行Prolog路由并列出列表 [英] Prolog routing between 2 points and making a list of it
本文介绍了在2点之间进行Prolog路由并列出列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一种方法来返回两点之间的路线中使用的所有道路*,这是我拥有的地图:
I need a method that returns all the roads* used in a route between two points, here's the map I have:
例如,要将A
指向E
,我需要使用的道路清单,如下所示:
Example, to point A
to E
, i need a list of roads used, like that:
route(a, e, R).
R = [1,5].
我在标记我已经访问过的路线时遇到了问题,最重要的是,在列表中注册使用的道路编号.
I'm having problems in marking the routes that i've already visited, and on top of that, register the number of the road used in a list.
所以这是我到目前为止的代码:
So here's my code so far:
road(1,a,b).
road(2,a,d).
road(3,b,c).
road(4,c,d).
road(5,b,e).
road(6,c,f).
road(7,d,f).
road(8,e,f).
connected(A,B) :- road(_,A,B).
route(A, B, R) :- road(R, A, B), road(R, B, A).
route(A, B, [R2|R]) :- route(A, C, R2), route(C, B, R2).
推荐答案
您的解决方案仍然无法抵抗图形中的循环,这是一次让您牢记的想法
Your solution is still not resisting cycles in graph, here is once that keeps it in mind
route(A,A,R,R).
route(A,B,R,R2) :- road(Rx,A,C), \+ member(Rx,R) , route(C,B,[Rx|R],R2).
route(A,B,R) :- route(A,B,[],Rx), reverse(R,Rx).
这篇关于在2点之间进行Prolog路由并列出列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文