- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Question: Fill in the grid with squares (of any size) that do not touch or overlap, even at the corners. The numbers below and at theright indicate the number of grid squares that are filled in thecorresponding column / row.
:- use_module(library(clpfd)).
:- include('utils.pl').
solve(Rows, Columns, Vars) :-
% Domain and variables definition
length(Rows, Size),
MaxNumSquares is Size * Size,
NumSquares #>= 0,
NumSquares #< MaxNumSquares,
length(StartsX, NumSquares),
length(StartsY, NumSquares),
length(SquareSizes, NumSquares),
S is Size - 1,
domain(StartsX, 0, S),
domain(StartsY, 0, S),
domain(SquareSizes, 1, Size),
construct_squares(Size, StartsX, StartsY, SquareSizes, Squares),
% Constraints
disjoint2(Squares, [margin(0, 0, 1, 1)]),
lines_constraints(0, Rows, StartsX, SquareSizes),
lines_constraints(0, Columns, StartsY, SquareSizes),
% Solution search
VarsList = [NumSquares, StartsX, StartsY, SquareSizes],
flatten(VarsList, Vars),
labeling([], Vars).
construct_squares(_, [], [], [], []).
construct_squares(Size, [StartX|T1], [StartY|T2], [SquareSize|T3], [square(StartX, SquareSize, StartY, SquareSize)|T4]) :-
StartX + SquareSize #=< Size,
StartY + SquareSize #=< Size,
construct_squares(Size, T1, T2, T3, T4).
% Rows and columns NumFilledCells cells constraints
lines_constraints(_, [], _, _).
lines_constraints(Index, [NumFilledCells|T], Starts, SquareSizes) :-
line_constraints(Index, NumFilledCells, Starts, SquareSizes),
I is Index + 1,
lines_constraints(I, T, Starts, SquareSizes).
line_constraints(Index, NumFilledCells, Starts, SquareSizes) :-
findall(
SquareSize,
(
element(N, Starts, Start),
element(N, SquareSizes, SquareSize),
intersect(Index, Start, SquareSize)
),
Lines),
sum(Lines, #=, NumFilledCells).
% Check if a square intersects a row or column
intersect(Index, Start, SquareSize) :-
Start #=< Index,
Index #=< Start + SquareSize.
最佳答案
问题出在您的 line_constraint/4
谓词。在其中,您在 findall/3
中发布了一些 clpfd 约束。 .这意味着这些约束仅在 findall/3
内有效。 .这是一种重写谓词的方法,以保持约束张贴(假设您使用的是 SICStus,我使用 do
循环样式,这只是递归谓词周围的语法糖):
line_constraints(Index, NumFilledCells, Starts, SquareSizes) :-
(
foreach(Start,Starts),
foreach(SquareSize,SquareSizes),
foreach(Usage,Usages),
param(Index)
do
Intersect #<=> ( Start #=< Index #/\ Index #< Start + SquareSize),
Usage #= Intersect * SquareSize
),
sum(Usages, #=, NumFilledCells).
(请注意,我将第二个不等式更改为严格的不等式:正方形的结尾正好在
Start + SquareSize
之前。)
lines_constraints/4
受一些累积约束。
关于prolog - 带约束规划的方形拼图问题解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65497652/
我在某处遇到了以下难题 #include int main() { { /*Fill in something here to make this code compile
我正在尝试为 iOS 创建一个拼图游戏应用程序。从我在互联网上的搜索来看,确实没有任何关于这个主题的教程。有谁知道任何人都知道的好教程或游戏教程的链接?谢谢。顺便说一下,iOS4 将不胜感激。 最佳答
如果必须使用 Promises,您会如何编写以下代码? 这个想法是,“私有(private)”方法 p1 调用一个执行异步操作的函数,然后,当异步调用的结果准备就绪时,控制权将传递给“私有(priva
下面是其中一个 facebook 谜题:我无法理解如何进行此操作。 你有 C 个容器、B 个黑球和无限数量的白球。您希望以一种方式在容器之间分配球,即每个容器至少包含一个球,并且选择白球的概率大于或等
有 5 位成员围坐在一张 table 旁。关键值是坐在 table 周围的成员数量。所以现在关键值将是 5。一个恐怖分子告诉成员,因为你们是 5 个成员,所以我将从第一个成员开始数,数到 5 的人将被
你能在不抛出错误的情况下解决这个问题吗?答案是单线。这是来自一个死的职位发布,在回复中要求回答。我认为这是剔除受访者的聪明方法,但我似乎无法在不出错的情况下回答它。 显而易见的解决方案: f.moo(
此源输出 G'Day Mate. 这是怎么发生的? public static void main(String args[]) { System.out.println("Hello Wor
我正在 android 中开发一个 slider 拼图,它有一个图像被分解成小图像,我们需要对这些 fragment 进行排序以形成正确的图像。我使用了一个 3x3 的 GridView ,其中包含
我遇到了以下难题,无法在 Picat 中制定解决方案: You will generate 5-digit numbers, where each digit is in 1..5 and diffe
我是 Javascript 新手,并且正在努力解决 CodeWars 中的这个难题。 约翰想用壁纸装饰房间。房间的尺寸为:宽度(w)、高度(h)、长度(l)。一卷壁纸的尺寸为 52cm 宽,10m 长
我对 Java 还很陌生,尝试过 Best Before puzzle from Spotify昨天。当我发送它时,我收到“错误答案”错误。检查其他解决方案没有帮助,我无法弄清楚哪个输入给出了错误的答
我正在尝试恢复我拥有的一些旧代码,这是一个拼图游戏。它从文件夹中加载图像(拼图),将它们随机放置在页面周围,然后拖放到板上。这曾经有效,但当我今天尝试使用它时,它只是抛出错误(见下文)。 HTML:
这对你们来说可能是个愚蠢的问题。它是关于 CSS Sprites 的。我有一个包含 4 个菜单的导航,例如 .. HOME COMPANY SERVICES SUPPORT 尽管我使用了一个 css
我需要创建一个标题,可以根据正在构建的页面轻松添加或删除部分,但我在处理其中一部分时遇到了问题。 我有一个标题,看起来像这样将所有组件放在 如果导航被移除,它应该看起来像这样(垂直居中) 我的问题是如
我在 JS 中构建了一个 15 拼图,但我的随机拼图生成正在创建无法解决的拼图实例。这可能是因为我不是计算机科学专业的负责人,但我不确定如何计算代码排列中的反转次数。我想知道如何编写我的代码,以便我可
我正在寻找 8 Puzzle graphs tree generator,最好是 (php+) html+css+javascript。我需要的是类似 3 2 1 6 8 7 5 4 会生成所有可
我住在德国,在 Android Market 上发布“Last Call Widget”。随着时间的推移,我一直在稳步改进它,但一组用户仍然提示它无法在他们的设备上运行。 我的小部件监听“androi
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试制作一个看起来像这样的拼图游戏。我试过的看起来像这样。 https://jsfiddle.net/uccfb46z/ 现在如果我想改变碎片的形状我需要修改这部分 - outside: fu
首先,让我为缺少 SSCCE 表示歉意。我在这方面真的没有足够的专业知识来弄清楚什么是相关的,什么不是。 简而言之,问题是在两台运行相同分辨率 (1366x768) 的不同计算机上,我女朋友的 tum
我是一名优秀的程序员,十分优秀!