prolog - 路径重复超过特定数量
问题描述
有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)
.
解决方案
推荐阅读
- kubernetes - 在不终止的情况下在完成时缩减副本
- c# - 如何从 C# 返回 Python 脚本结果而不打印?
- google-app-engine - 发布/订阅推送返回 503 用于基本缩放
- javascript - 如何从不带引号的 WebAPI 控制器发送带有 javascript 函数的 JSON
- node.js - 节点:仅当有值时如何向输入显示值
- php - Laravel 5.8+从查询中获取列和行数组
- node.js - 使用 vue 模板读取文件
- android - 我如何在 CalendarView 中显示我的目标名称?
- r - 根据另一列的值替换列中的 NA
- angularjs - 为什么在 AngularJS 中的 ng-repeat 中 ng-click 不会触发函数?