Algorithm of the Board overlay

Source: Internet
Author: User

Analysis and implementation of board coverage

I. What is a checkerboard overlay?

First of all to understand what is a special square in a 2^k*2^k of the board, if just a square and other squares, it is called a special box, it is obvious that special squares appear in the position of 4^k, that is k>=0, there are 4^k different special chessboard

Board Overlay: with 4 different L-shaped dominoes covering a given special chessboard (i.e. the position of a special square has been determined) to remove all the special square, and any two L-type dominoes must not be repeated coverage, according to the rules, we are easy to know, in the 2^K*2^K board cover, The number of L-type bone plate used is exactly (4^k-1)/3, i.e. (number of squares-special squares)/3

For example, a special chessboard for k=2 (three squares of the same color form an L-shaped domino) and 4 different forms of L-shaped dominoes

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

two. The idea and method of realizing the board coverage

The basic method of the Board covering implementation is the Division method, which is a brief introduction to the design of the board cover, what is the division method?

The basic idea of divide-and-conquer method: The problem of a scale n is decomposed into K small sub-problem, these sub-problems are independent and same as the original problem. Solve these sub-problems recursively, then combine the solutions of each sub-problem to get the solution of the original problem. To put it simply, the problem of scale n is decomposed from top to bottom, until the small problem is decomposed to small enough to be solved, and then merged from the base to get the original solution. Nxi

Analysis idea: When K〉0, the 2^k*2^k chessboard is divided into 4 2^k-1*2^k-1 sub-chessboard,

Special squares must be located in these four small chessboard, the remaining three sub-chessboard has no special squares, in order to convert the three non-special squares of the sub-chessboard to a special chessboard, we can use an L-shaped bone plate covering the three smaller chessboard rendezvous,

As can be seen from the figure, the three sub-chessboard is covered by the L-type dominoes as a special square on the board, thereby decomposing the problem into 4 smaller board coverage problems. Recursively use this method of partitioning until the chessboard is simplified to the 1*1 chessboard, ending the recursion.

To achieve the analysis of this algorithm: each time after the split four small squares to judge, to determine whether the special box is inside. The method of judging here is that each time you first record the row and column coordinates of the upper left corner of the block, and then compare it with the special grid coordinates, we can know if the special square is in the block. If the special block in the inside, this direct recursion to go down, if not, according to the different positions of the four blocks divided, the lower right corner, the lower left corner, the upper right corner or the upper left corner of the square marked as a special block, and then continue to recursion. In the recursive function, there is also a variable s to record the number of squares of the edge, each time the other block partition, the edge of the number of squares will be halved, this variable is to facilitate the determination of the location of special squares.

Three. The specific implementation code of the Board overlay

1#include <stdio.h>2#include <stdlib.h>3 4 intnum =0;5 intmatrix[ -][ -];6 voidChessboard (intTrintTcintBrjintdcintsize);7 intMain ()8 {9     intSize,r,c,row,col;Tenprintf"Please enter the number of the chessboard"); Onescanf"%d",&size); Aprintf"Please enter the column number of the special square"); -scanf"%d%d",&row,&col); -Chessboard (0,0, row,col,size); the  -      for(R =0; R < size; r++) -     { -          for(c =0; c < size; C++) +         { -printf"%2d", Matrix[r][c]); +         } Aprintf"\ n"); at     } -  -     return 0; - } -  - voidChessboard (intTrintTcintBrjintdcintsize) in { -  to     ints,t; +     if(size==1)return; -s = size/2;//Split Board thet = ++num;//L-type domino number *     if(Dr < tr + S && DC < TC +s)//overlay left upper corner sub-chessboard $     {Panax Notoginseng         //Special squares in this chessboard - chessboard (tr,tc,dr,dc,s); the     } +     Else            //there are no special squares in this chessboard A     { the         //cover the lower right corner with the T-shape L-shaped dominoes +matrix[tr+s-1][tc+s-1] =T; -         //cover the remaining squares $Chessboard (tr,tc,tr+s-1, tc+s-1, s); $     } -     //Overlay Right upper corner sub-chessboard -     if(Dr < tr + S && DC >= TC + s)//  the     { -         //Special squares in this chessboardWuyiChessboard (tr,tc+s,dr,dc,s); the     } -     Else      //there are no special squares in this chessboard Wu     { -         //cover the lower left corner with the T-shape L-shaped dominoes Aboutmatrix[tr+s-1][tc+s] =T; $         //cover the remaining squares -Chessboard (tr,tc+s,tr+s-1, tc+s,s); -     } -      //overlay left Lower corner sub-chessboard A     if(Dr >= tr + S && DC < TC +s) +     { the         //Special squares in this chessboard -Chessboard (tr+s,tc,dr,dc,s); $     }  the     Else the     { the         //cover the upper right corner with T-shape L-shaped dominoes thematrix[tr+s][tc+s-1] =T; -         //cover the remaining squares inChessboard (tr+s,tc,tr+s,tc+s-1, s); the     } the       //Overlay Right Bottom corner sub-chessboard About     if(Dr >= tr + S && DC >= TC +s) the     { the           //Special squares in this chessboard theChessboard (tr+s,tc+s,dr,dc,s); +     }  -     Else the     {Bayi         //cover upper left corner with T-shape L-shaped dominoes theMatrix[tr+s][tc+s] =T; the         //cover the remaining squares -Chessboard (tr+s,tc+s,tr+s,tc+s,s); -     } the  the}

Running results such as

Algorithm of the Board overlay

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.