Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog中的正方形放置_Prolog - Fatal编程技术网

Prolog中的正方形放置

Prolog中的正方形放置,prolog,Prolog,我有一个nxn区域。我想列出所有可能的KxKm正方形(k

我有一个nxn区域。我想列出所有可能的KxKm正方形(k 输入和输出如下:(k:小正方形的大小,n:大正方形的大小,m:小正方形的数量)


我发布了一个解决方案,展示了一个可能的Prolog编码,在样式生成和测试中。在某个位置,您可以放置适当的算术运算,以完成您的作业

%%  placing

place_squares(Big, Small, Squares) :-
    place_not_overlapping(Big, Small, [], Squares).

place_not_overlapping(Big, Small, SoFar, Squares) :-
    available_position(Big, Small, Position),
    \+ overlapping(Small, Position, SoFar),
    place_not_overlapping(Big, Small, [Position|SoFar], Squares).
place_not_overlapping(_Big, _Small, Squares, Sorted) :-
    sort(Squares, Sorted).

overlapping(Size, R*C, Squares) :-
    member(X*Y, Squares),
    ...  % write conditions here

available_position(Big, Small, Row*Col) :-
    Range is Big - Small + 1,
    between(1, Range, Row),
    between(1, Range, Col).
放置后,很容易显示

%%  drawing

draw_squares(Big, Small, Squares) :-
    forall(between(1, Big, Row),
           (forall(between(1, Big, Col),
               draw_point(Row*Col, Small, Squares)),
        nl
           )).
draw_point(Point, Small, Squares) :-
    (   nth1(I, Squares, Square),
        contained(Point, Square, Small)
    ) -> write(I) ; write('-').

contained(R*C, A*B, Size) :-
    ... % place arithmetic here
具有所需尺寸和图纸的结果

?- place_squares(5,2,Q),draw_squares(5,2,Q).
1122-
1122-
3344-
3344-
-----
Q = [1*1, 1*3, 3*1, 3*3] ;
1122-
1122-
33-44
33-44
-----
Q = [1*1, 1*3, 3*1, 3*4] ;
1122-
1122-
33---
3344-
--44-
Q = [1*1, 1*3, 3*1, 4*3] .
...
place_squares/3输出进行排序,以便于显示,也可用于消除对称性,并获得所有解决方案的计数:

9 ?- setof(Q, place_squares(5,2,Q), L), length(L, N).
L = [[], [1*1], [1*1, 1*3], [1*1, 1*3, 3*1], [1*1, 1*3, 3*1, ... * ...], [1*1, 1*3, ... * ...|...], [1*1, ... * ...|...], [... * ...|...], [...|...]|...],
N = 314.

您可以注意到,这接受具有“空闲”空间的电路板。您可以过滤掉这些不完整的解决方案,以完成您的任务。

您应该至少给出一个示例,说明您希望如何调用谓词以及您希望得到什么样的答案。另外,如果正方形有一个接触角,它们是否接触?不管怎样,如果没有编程,你会想到多少答案——你考虑对称性吗?(编辑你的问题,不要在评论中回答)。至于“把东西放在黑板上”,寻找解决N皇后问题的方法。方法应该是相同的,在使用和不使用约束编程(CLPFD)的情况下都有关于Stackoverflow的解决方案@Boris I编辑了这个问题
9 ?- setof(Q, place_squares(5,2,Q), L), length(L, N).
L = [[], [1*1], [1*1, 1*3], [1*1, 1*3, 3*1], [1*1, 1*3, 3*1, ... * ...], [1*1, 1*3, ... * ...|...], [1*1, ... * ...|...], [... * ...|...], [...|...]|...],
N = 314.