# Placing 1d items in a 2d grid with constraints

I'M looking for an algorithm (preferably polynomial) for the following problem:

Input:
- an M x N grid, where each square can be either empty or occupied
- a list of items
- a list of constraints

Output:
the grid, such that all items are placed and the constraints aren't violated.

Each item is a 1d block of length k. Different items may have different length, and there may be several items with the same length. Items can't ovelap each other.

Example 1: Given 1 x 5 grid and 2 items of lengths 1, 2 respectively. The following solutions are acceptable:

11100

10110

11010

and so forth.

Example 2: Given 3 x 5 grid and 2 items of lengths 1, 2 respectively. The following solutions are acceptable:

00110
10000
00000

11000
10000
00000

etc.

Possible constraints:
a. Isolation - items may not touch each other. e.g. the following solutions is illegal:

Example 1:

11100

Example 2:

11000
10000
00000

However this is a legal solution:

01100
10000
00000

i.e. touching diagonals are legal.

b. Facing - 2 items must be placed one in front of the other. e.g.

11000
00000
11000

What I did so far is to find a solution for a simpler problem, where the grid is 1d and the only possible constraint is Isolation. I solved it by representing it as a search problem, and using A* with the heuristic: sum of lengths of items not placed yet. The problem is that it suffers from state explosion which will only deteriorate in higher dimensions.

• This smells NP-hard. A reduction from partition could work, but I don't have time atm to work out the details. (I'd try with a $2 \times\frac m2$-grid for elements with a weight sum of $m$. Some adjustment might be necessary to forbid placing elements with size 2 in the wrong orientation.) May 4, 2014 at 21:42

The 1-dimensional case (a $1 \times n$ grid) is straightforward. I'm not sure why you are having difficulty finding solutions when the grid is 1-dimensional. If you don't have any constraints, then you just add up the lengths of the items; if their total length is $\le n$, you can solve it (just put the items next to each other), otherwise you can't. To deal with the isolation constraint, you can note that the problem with tiles lengths $\ell_1,\dots,\ell_k$ and an isolation constraint is solvable if and only if the problem with tile lengths $\ell_1+1,\dots,\ell_{k-1}+1,\ell_k$ and no isolation constraint is solvable.

The 2-dimensional case (a $m \times n$ grid) looks much harder. One approach, in practice, might be to use a SAT solver. Introduce boolean variables $x_{i,j,k,d}$ and $y_{i,j}$, with the intent that $x_{i,j,k,d}=1$ if the $k$th tile starts at location $(i,j)$ on the grid and proceeds in direction $d$, and $y_{i,j}=1$ if location $(i,j)$ is covered by some tile. Now you can formulate your problem as a set of boolean constraints on the $x$'s and $y$'s, then feed it to an off-the-shelf SAT solver. You could experiment with this for the parameter sizes that tend to crop up for you in practice and see how well it works. SAT solvers often work surprisingly well, though there is no guarantee: they are not a silver bullet, and the worst-case running time will remain exponential. There are many other ways to represent this as a SAT problem, too, so you could try other representations as well to see which tends to work best with existing SAT solvers.

I'm not sure you're going to be able to do much better than this: as FrankW says, for 2-dimensional grids this smells like it might be NP-complete in the general case.

• can you provide a quick illustration for the 2d formulation to the SAT solver please? Dec 29, 2020 at 5:47
• @evandrix, I'll let you work through an example yourself and see if you have any specific questions as you go through that exercise. There are lots of materials and tutorials on SAT solvers available on the Internet, if that's what you are looking for.
– D.W.
Dec 29, 2020 at 19:52
• my question was motivated by me looking at the answer here, which proposes the same representation as you (so perhaps this is THE standard transformation for such 2d problems to something a SAT solver can easily consume), and not understanding how to extend the step "Then, write clauses that prohibit two crossing segments. If segment 1 and segment 2 are both length 2, then we add the clause (!L_seg1_0_0_right || !L_seg2_1_0_right)" for >2 line segments, is it going to be N choose 2 i.e. pairwise, for N line segments? Dec 29, 2020 at 21:12
• if so, won't that be many separate constraints, would it choke the SAT solver, and produce non-viable runtime? Dec 29, 2020 at 21:14
• which is why i requested for a simple example, so that i can compare what you are proposing, vs what stackoverflow.com/questions/12099244#12099815 is proposing Dec 29, 2020 at 21:15