首页 > 解决方案 > 路径重复超过特定数量

问题描述

有8个房间,更具体的a室,b室,c室,d室,e室,f室,g室,h室。问题是,它向我展示了一个平局,并问我房间的每一扇门不应该重复超过 4 个。这个代码应该是怎样的?我所做的是:

 %the directions
    next_to(a,b).
    next_to(a,c).
    next_to(a,d)
    next_to(b,a).
    next_to(b,h).
    next_to(c,a).
    next_to(c,d).
    next_to(d,a).
    next_to(d,c).
    next_to(d,f).
    next_to(e,d).
    next_to(e,g).
    next_to(h,b).
    next_to(h,g).
    next_to(g,e).
    next_to(g,h).
    next_to(g,f).
    next_to(f,d).
    next_to(f,g).
    %i did for two ,i mean not to repeated more than 2 times, but the exercise asking me no more than 4
   %i did a loop here smaller than 4 ,i know its wrong.
    calc(L, NL):-
      include(in_range, L, NL).

    pathloop(Start, End, _, [Start, End]) :-
        next_to(Start, End).
    pathloop(Start, End, Visited, [Start|PathRest]) :-
        next_to(Start, Second),
        not(member(Second, Visited)),
        pathloop(Second, End, [Second|Visited], PathRest).



    in_range(X):-
      number(X),
      (X > 0 ; X < 4)

.

标签: prolog

解决方案


推荐阅读