4

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.

dimid
  • 141
  • 3

1 Answers1

1

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.

D.W.
  • 167,959
  • 22
  • 232
  • 500