Board coverage of the divide and conquer algorithm (complete code implementation)

Source: Internet
Author: User

I used a simplified method here, but it was code simplification or the sub-governance recursion idea. One is divided into 4, until 2*2 can be solved directly.

The placement of the four dominoes exactly corresponds to: dir [4] [2] = {0, 0}, {0, 1}, {1, 1}, {1, 0 }}; these four directions.

The four directions can be used to determine the four directions of the incomplete position (top left, top right, bottom right, and bottom left ).

Therefore, we can use a loop instead of judging the four directions in sequence to simplify the code.

You can also use a global variable title to represent the first few dominoes, and then use a title to replace ABCD.

Copy:

[Solution]: divide the 2 ^ k x 2 ^ K board into four equal sub-boards. The special square is located in one of the four, construct three sub-boards with no special squares left, and set one of them as a special one. If:

Upper left sub-board (if there is no special square) ---- assume that the square in the lower right corner of the sub-board is a special square.
The sub-board on the upper right (if there is no special square) ---- assume that the square in the lower left corner of the sub-board is a special square.
Lower left sub-board (if there is no special square) ---- assume that the square in the upper right corner of the sub-board is a special square.
Lower right sub-board (if there is no special square) ---- assume that the square in the upper left corner of the sub-board is a special square.

Of course, the above four types can only be true and there must be only three. The special squares of the three assumptions constitute an L-type skeleton, and we can mark them with the same mark. In this way, the four sub-boards are similar to the old large ones, so we can solve them with recursive algorithms.

// ================================================ ==================================================================== // Name: board coverage. CPP // Author: gaotong // version: // copyright: Your copyright notice // Description: A, B, C, and D indicate four types of dominoes. that is, // a B CC dd // AA BB c d // ============================ ========================================================== ==============

# Include <iostream> using namespace STD; int n, x, y; // board size, incomplete position X, ychar map [1000] [1000]; // chessboard array int dir [4] [2] = {0, 0}, {0, 1}, {1, 1}, {1, 0 }}; // put char pieces [4] = {'A', 'B', 'C', 'D'} for four types of chess '}; // four types indicate int title = 0; // style indicates the type of dominoes; R, t indicates the area where the dominoes are placed (2*2) void set_piece (INT style, int R, int c) {Title ++; For (INT I = 0; I <4; I ++) if (I = style) {// The placement mode of each style pair in Dir for (Int J = 0; j <4; j ++) if (I! = J) map [R + dir [J] [0] [C + dir [J] [1] = pieces [I] ;}// startr, position in the upper left corner of the starc (row, column) area; location where Dr and DC (row, column) are incomplete; region size void chessboard (INT startr, int startc, int DR, int DC, int size) {If (size = 1) return; int S = size/2; int RR = Dr> = startr + S; // 1 indicates int cc = Dc> = startc + S on the right; // 1 indicates for (INT I = 0; I <4; I ++) {If (dir [I] [0] = RR & dir [I] [1] = cc) {// Based on the incomplete location, place a card in the middle of the area. // For example, If RR = 0 cc = 0, that is, the incomplete position is in the upper left corner, corresponding to dir [0] = {0} // that is, style = 0, the first type of bone card set_piece (I, startr + s-1, startc + s-1); For (Int J = 0; j <4; j ++) {If (j = I) // place the 1/4 chessboard (startr + S * dir [J] [0] with an incomplete position, startc + S * dir [J] [1], Dr, DC, S); else {// display the remaining 3/4 respectively. the incomplete position refers to the position of the bone card placed in the center of the region in the current region (for example, for the upper right corner, the incomplete position is in the sitting position) chessboard (startr + S * dir [J] [0], startc + S * dir [J] [1], startr + s-1 + dir [J] [0], startc + s-1 + dir [J] [1], S) ;}}}int main () {cout <"welcome to the checkerboard overwrite program: "<Endl; cout <" each A, B, C, and D represent four dominoes in different directions: "<Endl; cout <"a B CC dd" <Endl; cout <"AA BB C D" <Endl; cout <"Enter three numbers, the Board size is n (less than 1000), the incomplete position is X, Y (between 1 and N): "<Endl; CIN> N> x> Y; // determine whether N is 2 to the Npower if (N & (n-1 )) | x> N | x <1 | Y <1 | Y> N) {cout <"the input is invalid" <Endl ;} else {chessboard (0, 0, x-1, Y-1, n); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {cout <map [I] [J] ;}cout <Endl ;}} return 0 ;}

Output:

Welcome to the checkerboard overwriting program: A, B, C, D represents four different directions of the dominoes: a B CC ddaa BB C D input 3 numbers, board size N (less than 1000), incomplete position X, Y (between 1 and N): 4 2 3 ccddcb dbbbabbaa

This is the code in the book (incomplete ):

Void chessboard (int tr, int TC, int DR, int DC, int size)

{If (size = 1) return;

Int T = tile ++, // Number of L-type dominoes

S = size/2; // split the Board

// Overwrite the sub-board in the upper left corner

If (Dr <tr + S & DC <TC + S)

// Special square in this board

Chessboard (TR, TC, Dr, DC, S );

Else {// This Game Board has no special square

// Use the T-shaped string to cover the lower right corner

Board [tr + s-1] [TC + s-1] = T;

// Overwrite the remaining squares

Chessboard (TR, TC, tr + s-1, TC + S-l, S );}

// Overwrite the sub-board in the upper right corner

If (Dr <= tr + S & DC> = TC + S)

// Special square in this board

Chessboard (TR, TC + S, Dr, DC, S );

Else {// This Game Board has no special square

// Use the T Bone card to overwrite the lower left corner

Board [tr + s-1] [TC + S] = T;

// Overwrite the remaining squares

Chessboard (TR, TC + S, tr + s-1, TC + S, S );}

.....................

Void outputboard (INT size)

{For Inti = 0; I <size; I ++ ){

For intj = 0; j <size; j ++ );

Cout <SETW (5) <Board [I] [J];

Cout <Endl ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.