4

For example, let's say we have a bounded 2D grid which we want to cover with square tiles of equal size. We have unlimited number of tiles that fall into a defined number of types. Each type of tile specifies the letters printed on that tile. Letters are printed next to each edge and only the tiles with matching letters on their adjacent edges can be placed next to one another on the grid. Tiles may be rotated.

Given the size of the grid and tile type definitions, what is the fastest method of arranging the tiles such that the above constraint is met and the entire/majority of the grid is covered? Note that my use case is for large grids (~20 in each dimension) and medium-large number of solutions (unlike Eternity II).

So far, I've tried DFS starting in the center and picking the locations around filled area that allow the least number of possibilities and backtrack in case no progress can be made. This only works for simple scenarios with one or two types. Any more and too much backtracking ensues.

Here's a trivial example, showing input and the final output:

Example

Mansour
  • 1,787
  • 2
  • 20
  • 33
  • could tell about the constraints , grid size ? number of type of tiles ? – advocateofnone Dec 31 '14 at 03:40
  • I think all those are inputs to the problem. Though if you could formally define the inputs (input grid of size `A`*`B`, `N` types of tiles, `M` different letters, any others...?) that would be good. Also, can tiles have repeats among their 4 edges, or are all 4 always different letters? (add any answers to these questions to the OP). – Mshnik Dec 31 '14 at 04:34
  • Yes, those are in fact the inputs. Also, yes, repeats are allowed so for instance a tile may have letter v on all edges. And in case it's the only type of tile, arranging them would be trivial. – Mansour Dec 31 '14 at 05:17
  • Please explain your problem with example. It would be better to understand and answer your problem. – devsda Dec 31 '14 at 05:41

2 Answers2

5

This is a hard puzzle.

The Eternity 2 was a puzzle of this form with a 16 by 16 square grid.

Despite a 2 million dollar prize, no one found the solution in several years.

The paper "Jigsaw Puzzles, Edge Matching, and Polyomino Packing: Connections and Complexity" by Erik D. Demaine, Martin L. Demaine shows that this type of puzzle is NP-complete.

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
1

Given a problem of this sort with a square grid I would try a brute force list of all possible columns, and then a dynamic programming solution across all of the rows. This will find the number of solutions, and with a little extra work can be used to generate all of the solutions.

However if your rows are n long and there are m letters with k tiles, then the brute force list of all possible columns has up to mn possible right edges with up to m4k combinations/rotations of tiles needed to generate it. Then the transition from the right edge of one column to the right edge of the next next potentially has up to m2n possibilities in it. Those numbers are usually not worst case scenarios, but the size of those data structures will be the upper bound on the feasibility of that technique.

Of course if those data structures get too large to be feasible, then the recursive algorithm that you are describing will be too slow to enumerate all solutions. But if there are enough, it still might run acceptably fast even if this approach is infeasible.

Of course if you have more rows than columns, you'll want to reverse the role of rows and columns in this technique.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • After a little experiment, it seems that this solution works best for when the number of solutions is low. Otherwise, for instance, column size of 15 for two tiles '1111' and '2221' (and all their rotations), results in 3.5 million distinct columns. My use case is toward high number of solutions and large grids. – Mansour Jan 01 '15 at 01:21
  • Right. This is guaranteed to find all solutions. But if there are many, then just looking for one randomly might be much more efficient. – btilly Jan 02 '15 at 16:07