# Analysis and implementation of chessboard coverage

## 1, What is a chessboard overlay?

In a chessboard composed of 2^k * 2^k squares, if exactly one square is different from other squares, the square is called a special square, and the chessboard is called a special chessboard. Obviously, there are 4^k situations where special squares appear on the chessboard, that is, k > = 0, and there are 4^k different special chessboards.

Chessboard covering: cover all squares except the special square on a given special chessboard (i.e. the position of the special square has been determined) with four different forms of L-shaped dominoes, and any two L-shaped dominoes shall not be covered repeatedly. According to the rules, it is easy to know that the number of L-shaped bone discs used in the 2^k*2^k chessboard coverage is exactly (4^k-1)/3, that is (number of all squares - number of special squares) / 3.

The following figure shows a special chessboard (three small squares of the same color form an L-shaped dominoes) and four different forms of L-shaped dominoes when k=2. The blue one is a special square:

A special chessboard when k=2                                           4 different types of L-shaped dominoes

## 2, Ideas and methods of realizing chessboard coverage

The basic method of chessboard coverage is divide and conquer. It is a simple algorithm for designing chessboard coverage. What is divide and conquer?

The basic idea of divide and conquer method: decompose a problem with scale n into k smaller subproblems, which are independent of each other and the same as the original problem. Solve these subproblems recursively, and then combine the solutions of each subproblem to obtain the solution of the original problem. In short, decompose the problem with scale n from top to bottom until the subproblem is decomposed to a small enough size to accommodate When it is easy to solve, it is merged from bottom to top to obtain the original solution.

Analysis idea: when k > 0, divide the 2^k*2^k chessboard into four 2^(k-1)*2^(k-1) sub chessboards, as shown in the figure:

The special checkerboard must be located in the four small checkerboards, and the other three sub checkerboards do not have special checkerboards. In order to convert the three sub checkerboards without special checkerboards into special checkerboards, we can cover the junction of the three smaller checkerboards with an L-shaped bone plate, as shown in the figure:

As can be seen from the figure, the squares covered by L-shaped dominoes on the three sub chessboards become the special squares on the chessboard, so the problem is decomposed into four small-scale chessboard coverage problems. This segmentation method is used recursively until the chessboard is simplified to 1 * 1 chessboard, and the recursion ends.

Analysis to realize this algorithm: judge the four small blocks after segmentation each time to judge whether the special square is inside. The judgment method here is to record the row and column coordinates of the square in the upper left corner of the whole large block each time, and then compare with the coordinates of the special square to know whether the special square is in the block. If the special square is inside, it is directly If not, mark the squares in the lower right corner, lower left corner, upper right corner or upper left corner as special squares according to the different positions of the divided four squares, and then continue the recursion. In the recursion function, there is also a variable s to record the number of squares on the edge. Each time the other block is divided, the number of squares on the edge will be halved. This variable is for square Then determine the position of the special grid.

## 3, Chessboard coverage of the specific implementation code

```#include <stdio.h>
2 #include <stdlib.h>
3
4 int num = 0;
5 int Matrix[100][100];
6 void chessBoard(int tr, int tc, int dr, int dc, int size);
7 int main()
8 {
9     int size,r,c,row,col;
10    printf("Please enter the row and column number of the chessboard");
11     scanf("%d",&size);
12     printf("Please enter the row and column number of the special grid");
13     scanf("%d %d",&row,&col);
14     chessBoard(0,0,row,col,size);
15
16     for (r = 0; r < size; r++)
17     {
18         for (c = 0; c < size; c++)
19         {
20             printf("%2d ",Matrix[r][c]);
21         }
22         printf("\n");
23     }
24
25     return 0;
26 }
27
28 void chessBoard(int tr, int tc, int dr, int dc, int size)
29 {
30
31     int s,t;
32     if (size==1) return;
33     s = size/2;    //Split chessboard
34     t = ++num;      //L-shaped dominoes
35     if (dr < tr + s && dc < tc +s)                //Cover the upper left corner of the checkerboard
36     {
37         //Special squares in this chessboard
38         chessBoard(tr,tc,dr,dc,s);
39     }
40     else            //There are no special squares in this chessboard
41     {
42         //Cover the lower right corner with t-shaped L-shaped dominoes
43         Matrix[tr+s-1][tc+s-1] = t;
44         //Cover the remaining squares
45         chessBoard(tr,tc,tr+s-1,tc+s-1,s);
46     }
47     //Cover the upper right corner of the checkerboard
48     if (dr < tr + s && dc >= tc + s )           //
49     {
50         //Special squares in this chessboard
51         chessBoard(tr,tc+s,dr,dc,s);
52     }
53     else      //There are no special squares in this chessboard
54     {
55         //Cover the lower left corner with a t-shaped L-shaped dominoes
56         Matrix[tr+s-1][tc+s] = t;
57         //Cover the remaining squares
58         chessBoard(tr,tc+s,tr+s-1,tc+s,s);
59     }
60      //Cover the lower left checkerboard
61     if (dr >= tr + s && dc < tc + s)
62     {
63         //Special squares in this chessboard
64         chessBoard(tr+s,tc,dr,dc,s);
65     }
66     else
67     {
68         //Cover the upper right corner with t-shaped L-shaped dominoes
69         Matrix[tr+s][tc+s-1] = t;
70         //Cover the remaining squares
71         chessBoard(tr+s,tc,tr+s,tc+s-1,s);
72     }
73       //Cover the lower right checkerboard
74     if (dr >= tr + s && dc >= tc + s)
75     {
76           //Special squares in this chessboard
77         chessBoard(tr+s,tc+s,dr,dc,s);
78     }
79     else
80     {
81         //Cover the upper left corner with t-shaped L-shaped dominoes
82         Matrix[tr+s][tc+s] = t;
83         //Cover the remaining squares
84         chessBoard(tr+s,tc+s,tr+s,tc+s,s);
85     }
86
87 }
```

The operation results are as follows:

Reference:
1,https://www.cnblogs.com/crx234/p/5988055.html
2. Computer algorithm design and analysis Wang Xiaodong fifth edition. (if you need the answers to the exercises after class, you can leave a message in the comment area, and I will send a link)

Posted on Thu, 25 Nov 2021 16:29:28 -0500 by mevasquez