Prolog中的路线规划 [英] Route planning in Prolog

查看:70
本文介绍了Prolog中的路线规划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在线教程给出的路线规划程序:-

Here is a route planning program given by an online tutorial:-

route(X,Y,R) :-
 route(X,Y,[X],R).

route(X,Y,_,[drive(X,Y)]) :-
 travel(X,Y).
route(X,Y,V,[drive(X,Z)|R]) :-
 travel(X,Z),
 \+ member(Z,V),
 route(Z,Y,[Z|V],R)
 Z \= Y.     %Only required if Y is not ground.


travel(X,Y) :- road(X,Y).
travel(X,Y) :- road(Y,X). 


road(arad,sibiu).
road(arad,timisoara).
road(arad,zerind).
road(zerind,oradea).
road(oradea,sibiu).
road(sibiu,fagaras).

我不明白的是注释语句:Z\=Y.为什么需要这个声明?为什么只有在 Y 不接地时才需要该语句?

What I do not understand is the commented statement: Z\=Y. Why this statement is needed? And why that statement is only needed if Y is not ground?

推荐答案

Z \= Y 线可防止路径中出现循环,否则您可能会有 arad -> arad 的段.

The Z \= Y line prevents loops in the route otherwise you could have a segement that was arad -> arad.

这篇关于Prolog中的路线规划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆