# Square packing variant

I saw the following problem and was looking for references about the problem. The problem is stated as

“The green field is the empty area, the dark green 2x2 blocks are trees and the grey area is the off area. I’m wondering how to place the blocks in the field, while cutting the least amount of trees.”

with a picture (I've reduced the size) As far as I can tell this is related to square packing (https://en.wikipedia.org/wiki/Packing_problems#Packing_squares) but there is the related problem of minimizing the number of trees cut. I also found this Stack Exchange answer which suggested some approaches (https://stackoverflow.com/questions/1213394/what-algorithm-can-be-used-for-packing-rectangles-of-different-sizes-into-the-sm) but it doesn't fit exactly.

Any insights would be useful.

Thank you.

You can solve the problem via integer linear programming as follows. For each size $$s\in\{3,4,5\}$$, let $$n_s$$ be the number of blocks of size $$s$$ to place. For each cell $$(i,j)$$ and each size $$s \in \{3,4,5\}$$, let binary variable $$x_{i,j,s}$$ indicate whether $$(i,j)$$ is the top left corner of a block of size $$s$$. For each cell $$(i,j)$$, let $$B_{i,j}$$ be the set of blocks that cover that cell, and let binary variable $$y_{i,j}$$ indicate whether that cell is covered by some block. For each tree $$t$$, let $$C_t$$ be the cells that are covered by that tree, and let binary variable $$z_t$$ indicate whether that tree is cut. The problem is to minimize $$\sum_t z_t$$ subject to linear constraints \begin{align} \sum_{i,j} x_{i,j,s} &=n_s &&\text{for all s} \tag1 \\ \sum_{(i',j',s)\in B_{i,j}} x_{i',j',s} &=y_{i,j} &&\text{for all (i,j)} \tag2 \\ y_{i,j} &\le z_t &&\text{for all t and (i,j)\in C_t} \tag3\\ \end{align} Constraint $$(1)$$ places all $$n_s$$ blocks of size $$s$$. Constraint $$(2)$$ prevents overlapping blocks. Constraint $$(3)$$ enforces cutting a tree if some block covers a tree cell.
If I have entered the data correctly, the minimum turns out to be $$13$$, as shown here, where T indicates a surviving tree, C indicates a cut tree, $$3\times 3$$ blocks are red, $$4\times 4$$ blocks are orange, $$5\times 5$$ blocks are blue, and uncovered cells are green: 