Compute the polyomino capacity of a rectangle

7

1

Write a program or function that takes as input three positive integers x, y, and a and returns or outputs the maximum number of whole a✕1 rectangles that can be packed (axis-aligned) into an x✕y rectangle. Although the result will always be an integer, the program need not give the result using an integer type; in particular, floating-point outputs are acceptable. Use of standard loopholes is forbidden. Answers are scored in bytes including the cost of non-standard compiler flags or other marks.

Sample inputs and outputs:

 x,  y,  a -> result
--------------------
 1,  1,  3 ->      0
 2,  2,  3 ->      0
 1,  6,  4 ->      1
 6,  1,  4 ->      1
 1,  6,  3 ->      2
 6,  1,  3 ->      2
 2,  5,  3 ->      2
 5,  2,  3 ->      2
 3,  3,  3 ->      3
 3,  4,  3 ->      4
 4,  3,  3 ->      4
 4,  4,  3 ->      5
 6,  6,  4 ->      8
 5,  7,  4 ->      8
 7,  5,  4 ->      8
 6,  7,  4 ->     10
 7,  6,  4 ->     10
 5,  8,  3 ->     13
 8,  5,  3 ->     13
12, 12,  8 ->     16
12, 13,  8 ->     18
13, 12,  8 ->     18
 8,  8,  3 ->     21
12, 12,  5 ->     28
13, 13,  5 ->     33

In response to a question in the comments, note that some optimal packings may not be obvious at first glance. For instance, the case x=8, y=8, and a=3 has solutions that look like this:

||||||||
||||||||
||||||||
---||---
---||---
|| ||---
||------
||------

(8 + 2 + 2 = 12 vertical placements and 2 + 3 + 4 = 9 horizontal placements gives 12 + 9 = 21 placements in total.)

Edward

Posted 2016-08-18T01:17:25.463

Reputation: 495

So many links to meta... o.O – Leaky Nun – 2016-08-18T01:27:45.090

Maybe I'm just not seeing it but how do you fit 21 3x1's into an 8x8 grid? – Dendrobium – 2016-08-18T01:29:58.653

1I feel like I've seen this question beforw. Lemme search. – xnor – 2016-08-18T01:39:40.437

1Nice first question (even if confirmed to be duplicate)! – Leaky Nun – 2016-08-18T01:41:37.870

@Edward Yep, thanks for the clarification! – Dendrobium – 2016-08-18T02:00:32.383

1Not finding a dupe. I seem to remember something with fitting 1x4's, so having variable sizes would make this different. – xnor – 2016-08-18T02:13:45.587

2Must the rectangles be placed axis-aligned? – xnor – 2016-08-18T02:37:48.030

@xnor Let's say yes, axis-aligned. – Edward – 2016-08-20T14:29:58.770

Answers

8

Python 2, 53 bytes

lambda p,q,n:p/n*q+q/n*(p%n)-min(0,n-p%n-q%n)*(p>n<q)

Adapts the formula given in this paper, which is:

def f(p,q,n):a=p%n;b=q%n;return(p*q-[a*b,(n-a)*(n-b)][a+b>=n and p>=n and q>=n])/n

xnor

Posted 2016-08-18T01:17:25.463

Reputation: 115 687