如何让我的迷宫程序创建两个路由列表并将其打印到控制台? [英] How can I get my maze program to create and print two route-lists to the console?

查看:44
本文介绍了如何让我的迷宫程序创建两个路由列表并将其打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

link(entry, a).链接(a,b).链接(b,c).链接(c,d).链接(d,e).链接(b,e).链接(e,f).链接(f,c).链接(f,退出).路线(1, 2) :-成员(1, [entry,a,b,c,d,e,f,exit]),成员(2,[entry,a,b,e,f,exit]).路线(X, Z, [entry,a,b,c,d,e,f,exit]) :- 路线(X, Z,[R],[entry,a,b,c,d,e,f,出口]).路线(X, Z, [exit,f,e,d,c,b,a,entry], [entry,a,b,c,d,e,f,exit]) :-reverse(X, Y, [exit,f,e,d,c,b,a,entry], [entry,a,b,c,d,e,f,exit]),路线(Y,Z),写(X).

尽管阅读了数小时,但我仍在努力理解如何让我的程序生成并在控制台窗口中显示列出的路径.有没有人可以提供建议?我基本上没有编程经验,prolog 可能是我的大部分知识,这还不够.

解决方案

route(X, Y, [X, Y]) :- link(X,Y).路线(X,Y,[X | TY]):-链接(X,T),路线(T,Y,TY).

使用上面的route,下面的代码按照长度递增的顺序搜索路径.

?- length(X, _), route(entry,exit, X).X = [进入,a,b,e,f,退出];X = [进入,a,b,c,d,e,f,退出];X = [进入,a,b,e,f,c,d,e,f,退出];X = [进入,a,b,c,d,e,f,c,d,e,f,退出];X = [进入,a,b,e,f,c,d,e,f,c,d,e,f,退出]

由于我们没有约束 route 谓词以禁止重复节点,因此我们在路径的较长长度上有循环.

编辑:以下工作 SWI-Prolog 检查您的系统是否有 dif/2.在这里使用 maplist 允许我们进行增加路径长度的搜索.

route(X, Y, [X, Y]) :- link(X,Y).路线(X, Y, [X|TY]) :-链接(X,T),maplist(dif(X), TY), % X 不同于TY中的所有节点路线(T,Y,TY).

如果您没有 dif,请在 route(T, Y, TY) 之后使用 \+ member(X, TY).
这给

<预><代码>?- 路线(入口,出口,X).X = [进入,a,b,e,f,退出];X = [进入,a,b,c,d,e,f,退出];

在几个解决方案之后,它将无休止地循环.如果您希望阻止这种情况发生,您可以将路径长度限制为现有节点的数量

?- between(2, 8, N), length(X, N), route(entry, exit, X).N = 6,X = [进入,a,b,e,f,退出];N = 8,X = [进入,a,b,c,d,e,f,退出];错误的.

link(entry, a).
link(a, b).
link(b, c).
link(c, d).
link(d, e).

link(b, e).
link(e, f).
link(f, c).

link(f, exit).

route(1, 2) :-
member(1, [entry,a,b,c,d,e,f,exit]),
member(2, [entry,a,b,e,f,exit]).

route(X, Z, [entry,a,b,c,d,e,f,exit]) :- route(X, Z,[R],[entry,a,b,c,d,e,f,exit]).
route(X, Z, [exit,f,e,d,c,b,a,entry], [entry,a,b,c,d,e,f,exit]) :-
reverse(X, Y, [exit,f,e,d,c,b,a,entry], [entry,a,b,c,d,e,f,exit]),
route(Y, Z),

write(X).

Despite hours of reading, I am struggling to understand how I can make my program generate and display the listed paths in the console window. Is there anyone who can provide advice? I have basically no programming experience, prolog is probably the bulk of my knowledge, and that's insufficient.

解决方案

route(X, Y, [X, Y]) :- link(X,Y).
route(X, Y, [X | TY]) :- 
    link(X, T),
    route(T, Y, TY).

With route as above, the following code searches for the path in increasing order of length.

?- length(X, _), route(entry,exit, X).
X = [entry, a, b, e, f, exit] ;
X = [entry, a, b, c, d, e, f, exit] ;
X = [entry, a, b, e, f, c, d, e, f, exit] ;
X = [entry, a, b, c, d, e, f, c, d, e, f, exit] ;
X = [entry, a, b, e, f, c, d, e, f, c, d, e, f, exit]

Since we did not constrain route predicate to disallow repeated nodes, we have loops at higher lengths of the path.

EDIT: The following works SWI-Prolog check if your system has dif/2. Using maplist here allows us to do increasing path length search.

route(X, Y, [X, Y]) :- link(X,Y).
route(X, Y, [X|TY]) :-
    link(X, T),
    maplist(dif(X), TY), % X is different from all nodes in TY
    route(T, Y, TY).

If you do not have dif use \+ member(X, TY) after the route(T, Y, TY).
This gives


?- route(entry, exit, X).
X = [entry, a, b, e, f, exit] ;
X = [entry, a, b, c, d, e, f, exit] ;

After the couple of solutions it will loop endlessly. If you want that to stop that happening you can constrain the length of path to number of existing nodes

?- between(2, 8, N), length(X, N), route(entry, exit, X).
N = 6,
X = [entry, a, b, e, f, exit] ;
N = 8,
X = [entry, a, b, c, d, e, f, exit] ;
false.

这篇关于如何让我的迷宫程序创建两个路由列表并将其打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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