2

Short description:

I'm trying to generate tilings of a square with dominoes or in other words with 2x1 and 1x2 tiles.

Sometimes my algorithm puts a vertical tile in a way, that makes it impossible to fill the last row.

My current approach is to initialise a grid with 0's (say a 8x8 grid). I represent the left half of a horizontal tile with 1 and the right half with 2 in the grid.

Accordingly the upper half of vertical tile is 3 and the lower half is 4.

Then I go through each row of the grid from left to right and where a 0 is i put a tile:

  • Whenever I'm at the right edge of the grid or the space to the right is not 0, i put a vertical tile (3).
  • When I'm at the bottom edge, i (can) only put horizontal tiles.
  • When none of the above has set a value i put a random value (either 1 or 3).

Whenever I put a tile (1 or 3), i also put the corresponding half accordingly for the adjacent space of the grid. Say i put a 1 at (i,j) i then put a 2 at (i,j+1).

StoneMatrix = int[8][8];

for (int i = 0; i < StoneMatrix.length; i++) {
    for (int j = 0; j < StoneMatrix.length; j++) {
        if (StoneMatrix[i][j] != 0){
            //field already set as a tile
            continue;
        }

        if (i == StoneMatrix.length - 1) {
            //bottom row
            StoneMatrix[i][j] = 1;
            StoneMatrix[i][j + 1] = 2;
            continue;
        }

        if (j == StoneMatrix.length - 1) {
            //right edge
            StoneMatrix[i][j] = 3;
            StoneMatrix[i + 1][j] = 4;
            continue;
        }

        if (StoneMatrix[i][j + 1] != 0) {
            //field to the right taken.
            StoneMatrix[i][j] = 3;
            StoneMatrix[i + 1][j] = 4;
            continue;
        }

        StoneMatrix[i][j] = putOneOrThreeRandomly();
        putCorrespondingAdjacentTile();
    }
}

I feel like there has to be an easy or easier way to generate such tiling, but i couldn't find one yet. What im looking for is either some source, where such a generating algorithm is described or an easy solution, that fixes my error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
N. Ecker
  • 21
  • 4

2 Answers2

2

Optimal Domino Tiling

Create a random maximum tiling of any n by m chessboard with 2x1 dominos. When n*m is even, the tiling will be perfect.

Theory

Consider the chessboard's white and black squares. A domino must cover both a white and black square. Therefore if some perfect tiling exists, then the solution can be transformed into a graph as follows: each chess square becomes a vertex, and if a domino tile covers two adjacent tiles, the two vertices are connected. This would result in a bipartite (all edges have one white and one black vertex) and (maximally) matching graph.

Therefore, given any sized grid (or any shape for that matter), if we are able to solve the maximum cardinality matching problem in a bipartite graph, we can convert the solution into a domino tiling.

Implementation (Proof of Concept)

To solve the aforementioned graph problem, convert it into a max flow problem, and solve with Ford Fulkerson. Connect source node to all white tiles, and sink node to all black tiles. A white tile points to a black tile only if they are neighbors on the chess grid. Solve with all edges having capacity one. The utilized edges between nodes indicate that a domino will be placed there.

Example

Consider a chessboard 2 rows deep and 4 columns wide. The white squares are {0, 1, 2, 3}, and the black squares are {4, 5, 6, 7}

+---+---+---+---+
| 0 | 4 | 1 | 5 |
+---+---+---+---+
| 6 | 2 | 7 | 3 |
+---+---+---+---+

Now assume we have two additional nodes, 8 the source and 9 the sink. Then the graph to find the Max flow for becomes the below. Note that all edges have capacity 1.

0: 4, 6
1: 4, 5, 7
2: 4, 6, 7
3: 5, 7
4: 9
5: 9
6: 9
7: 9
8: 0, 1, 2, 3

After running Ford-fulkerson (with a randomized DFS for interesting solutions--use BFS if you want a boring row-wise solution each time), we might find the following edges used. Note that edges involving source/sink node have been omitted because they're not useful for reconstructing the tiling.

0-4
2-6
1-7
3-5

Finally, this corresponds to the following tiling

+-----+---+---+
| 0 4 | 1 | 5 |
+-----+   |   |
| 6 2 | 7 | 3 |
+-----+---+---+

I've also created a proof of concept in Java here: https://github.com/forsythe/domino_tiling. If you want to see it in action, here's a 16x16 output:

sample of 16x16 output generated by below method

nnnte
  • 69
  • 6
0

I "solved" the problem by setting a boolean to true, everytime I get to a state where you can no longer fill the Grid correctly.

Then at the end of the Generating Method if the boolean is false the grid is valid or if the boolean is true I restart the method. Basically a dirty "trial and error" I think.

Thats not the solution that I wanted but it works. So I'm leaving this question open.

N. Ecker
  • 21
  • 4