Board cover (divide and conquer), Board cover divide and conquer
Question: In a checker consisting of 2 ^ k x 2 ^ k squares, if there is a square different from other squares, it is called a special square, this Board is also called a special board. Now we need four different types of L-shaped bone cards to cover all squares except the special square on a given special board, and no two L-shaped bone cards can overlap. What is an L-shaped bone card? It is an angle consisting of three squares. There are four types;
Idea: divide a board into four small boards and split them along the midpoint of each side. The special Square must be located in one of the four smaller boards, there are no special squares in the remaining three sub-boards. In order to convert these three sub-boards without special sub-boards into special ones, we can use an L-type bone card to cover the three smaller ones and then recursively go down, terminate when the side length of the board is 1;
The Code is as follows:
# Include <iostream> # include <algorithm> # include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h ># include <set> # include <queue> # include <vector> using namespace std; int tot; int board [105] [105]; void chessboard (int tr, int tc, int dr, int dc, int size) {if (size = 1) return; int t = tot ++; int s = size/2; if (dr <tr + s & dc <tc + s) chessboard (tr, tc, dr, dc, s ); else {board [tr + s-1] [tc + s-1] = t; chessboard (t R, tc, tr + s-1, tc + s-1, s);} if (dr <tr + s & dc> = tc + s) chessboard (tr, tc + s, dr, dc, s); else {board [tr + s-1] [tc + s] = t; chessboard (tr, tc + s, tr + s-1, tc + s, s) ;}if (dr >= tr + s & dc <tc + s) chessboard (tr + s, tc, dr, dc, s ); else {board [tr + s] [tc + s-1] = t; chessboard (tr + s, tc, tr + s, tc + s-1, s );} if (dr> = tr + s & dc> = tc + s) chessboard (tr + s, tc + s, dr, dc, s ); else {board [tr + s] [tc + s] = t; chessboard (tr + s, tc + s, tr + s, tc + s, s );}} int main () {int dr, dc, s; Printf ("input format: Special grid horizontal coordinates, ordinate Board side length \ n"); while (scanf ("% d", & dr, & dc, & s )! = EOF) {tot = 1; memset (board, 0, sizeof (board); chessboard (0, 0, dr, dc, s); for (int I = 0; I <s; I ++) {for (int j = 0; j <s; j ++) printf ("%-2d ", board [I] [j]); cout <endl ;}} return 0 ;}
Run: