In a checkerboard consisting of a 2^k * 2^k, there is a square that is different from the other, and how to cover the other squares of this particular square, using the following four kinds of L-shaped dominoes.
Four L-type dominoes such as 1
Figure 1
Special squares in the chessboard 2
Figure 2
The basic principle of the implementation is to divide the chessboard of the 2^k * 2^k into four 2^ (k-1) * 2^ (K-1), the special squares must be in one of the sub-chessboard, if special squares in a certain sub-chessboard, continue to process the sub-chessboard recursively, Until there's only one square in the chessboard. If the special box is not in a certain sub-chessboard, the corresponding position in the sub-chessboard is set as a bone, and the checkerboard without special squares is converted to a sub-chessboard with special squares, and then the sub-chessboard is processed recursively. The above principle is shown in 3.
Figure 3
Saves the board in a two-dimensional array. The domino number starts from 1, the special box is 0, if it is a 4 * 4 chessboard, the Special square is (2,2), then the output of the program is
2 2 3 3
2 1 1 3
4 1 0 5
4 4 5 5
The same number as the same dominoes.
=============================================================================
Java code
package recursion and partition;
public class Chessboardcoverage {
private int tile = 1; The value of the L-shaped dominoes
private int board[][] = new INT[16][16]; Represents a chessboard
/*
* TR: The line number of the upper-left square of the Checkerboard TC: The column number of the upper left corner of the chessboard DR: the line number of the special square is DC: the column number of the special square size: The side length of the square checkerboard
*/
private void Coveragechessboard (int tr, int tc, int dr, int dc, int size) {
if (size = = 1)
Return
int t = tile++, s = SIZE/2;
Overlay left upper Corner sub-chessboard
if (Dr < tr + S && DC < TC + s)
Special squares in this chessboard
Coveragechessboard (TR, TC, DR, DC, s);
else//This chessboard has no special squares
{
Cover the lower right corner of the upper left-hand sub-chessboard with the T-shape L-type bone
BOARD[TR + S-1][TC + s-1] = t;
Overwrite the remaining squares in the upper left corner
Coveragechessboard (TR, TC, TR + s-1, TC + s-1, s);
}
Overlay right upper corner sub-chessboard
if (Dr < tr + S && DC >= TC + s)
Coveragechessboard (TR, TC + S, Dr, DC, s);
else {
Cover the lower left corner of the upper right-hand sub-chessboard with the T-shape L-shaped bone
BOARD[TR + S-1][TC + s] = t;
Overwrite the remaining squares in the upper right corner
Coveragechessboard (TR, TC + S, tr + s-1, TC + S, s);
}
Overlay left lower corner sub-chessboard
if (Dr >= tr + S && DC < TC + s)
Coveragechessboard (tr + S, TC, DR, DC, s);
else {
Cover the upper right corner of the lower left-hand checkerboard with the T-shaped bone
BOARD[TR + S][TC + s-1] = t;
Cover the remaining squares in the lower left corner
Coveragechessboard (tr + S, TC, TR + S, TC + s-1, s);
}
Overlay Right Bottom corner sub-chessboard
if (Dr >= tr + S && DC >= TC + s)
Coveragechessboard (tr + S, TC + S, Dr, DC, s);
else {
Cover the upper-left corner of the lower-right sub-chessboard with the T-shape L-shaped bone card
BOARD[TR + S][TC + s] = t;
Cover the remaining squares in the lower right corner
Coveragechessboard (tr + S, TC + S, tr + S, TC + S, s);
}
}
private void Displayboard (int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; J < size; J + +)
System.out.printf ("%4s", Board[i][j] + "");
System.out.println ();
}
}
public static void Main (string[] args) {
Chessboardcoverage chessboardcoverage = new Chessboardcoverage ();
Chessboardcoverage.coveragechessboard (0, 0, 2, 2, 4);
Chessboardcoverage.displayboard (4);
}
}
Checkerboard Coverage (recursive divide-and-conquer problem)