The Board coverage problem of computer algorithm design and analysis

Source: Internet
Author: User

First, the primer

Recently again on the algorithm class, now want to a bit ashamed, the university has studied a semester, but still feel just the teacher told the topic to understand, and did not learn some of the algorithm's good analytical methods and ideas, encountered a new problem often feel very tricky, after a bitter experience after learning to learn again , to be able to understand the ideas and core of each algorithm, but also exhort colleagues to work to the ground, can not cope with the teacher's homework, the last disadvantage or self ah.


second, the board coverage problemIn a chessboard made up of 2^k *2^k squares, a square is different from the other squares, which is called a special square, and is called the Board
For a special chessboard. There are four kinds of L-shaped dominoes, as shown, to cover all the squares except special squares with these four dominoes, and two L-type dominoes cannot cover each other.       Three, the thinking of solving problemsone of our common ideas for complex problems is simplifying the problem and simplifying it to the point where we can see the answer at a glance, as well. when k=1, the problem is reduced to a 2*2 checkerboard problem, because only four lattice, and contains a special lattice, so it can only be covered with a corresponding L-type domino, the problem is very simple. in thisWe redefine four kinds of L-type dominoes:


in the chessboard, we use (row, column) to represent a lattice, because (x, y) This representation is ambiguous for the image processing person, we prefer to think that the first dimension is a row, the second dimension is a column. Assuming that in the 2*2 Board, the special lattice appears in the seat (0,0), we want to use the 0 Domino cover the rest of the position, in the same vein, assuming that the special lattice appears in (0,1) This position, we have to use the 1-type dominoes cover the rest of the position, more general, Assuming that the special lattice appears in the 2*2 (row,col) position, we are going to use the Row*2+col Domino to cover the rest of the position, and both row and Col are indexed starting at 0.
when k=2, the problem becomes a 4*4 checkerboard problem, this time the problem is slightly complicated, we think ah, if you can turn it into a 2*2 board question how good ah, well, we will divide it into four 2*2 of the sub-chessboard, for that has a special lattice of the 2*2 sub-chessboard, quickly change can be resolved, What about the remaining three? Let's draw a picture and take a good look.

assuming that the special lattice appears at (0,2) This position, 3, then for the upper right corner of the sub-chessboard containing the special lattice we fill with the 0-type dominoes, 4. So the remaining three sub-chessboard, this time we found that the upper-left corner can only cover 3 and 2, the other two will have the remaining space, if the 2-type Domino, the back of the lower left corner must not be completely covered (you can try), you can only use the 3-type Domino cover, and so on, We can also cover the lower left corner and the lower right corner at this time only three squares left are not covered, as shown in 5. Now carefully observing the remaining three squares, we found that they are separated in three sub-chessboard, then these empty lattice in the sub-chessboard is not directly covered, because each sub-chessboard only one empty lattice, we can not make this empty lattice as a special lattice, So the four sub-chessboard is a small chessboard containing a special lattice, so that the original problem becomes four of the same sub-problem, and then solve each sub-chessboard, we then the three false sub-checkerboard lattice (6).

So how to choose the space as a special lattice of the sub-chessboard, by observing we found that for the sub-chess containing special lattice we do not have to specify a special lattice, for the remaining three sub-chessboard, we specify four sub-chessboard of the junction of the lattice as a special lattice.

Iv. InductionNow we summarize our solution: First of all, the big chessboard is divided into four 2^ (k-1) *2^ (k-1) of the sub-chessboard, and then no special lattice of the sub-chessboard designated false special lattice position, the original problem is decomposed into four sub-problems to solve, Of course, four sub-problems may not be directly solved, there may be a continuation of the recursive solution, assuming that the four sub-problems have been solved, we should use a specific L-shaped dominoes cover three false special lattice, so that the entire large board has been solved. we will think of the entire solution process, first of all to simplify the problem, simplified to directly see the point of the answer, and then analyze a slightly more complex situation, summarize the law, using the idea of divided treatment , the problem is resolved into four sub-problems, solve four sub-problems, respectively, In the process of solving sub-problems, there may be sub-problems, continuous recursive solution, and eventually each sub-problem solved, big problem also solved.
Five, code implementation
#include <iostream> #include <memory.h>using namespace Std;int **chessboard;int k=1;int length=0;int Bluerow=-1;int bluecol=-1;void init (), void Fillboard (int **_chessboard,int r,int c,int type), void fillchessboard (int * *     _chessboard,int k,int blue_row,int blue_col,int baserow,int basecol); void output (int **_chessboard); int main () {init ();    Fillchessboard (chessboard,k,bluerow,bluecol,0,0);    Output (chessboard);    for (int i=0;i<length;i++) {delete [] chessboard[i];    } Delete chessboard; return 0;}    void Init () {cout<< "Please input number k:" <<endl;    cin>>k;    cout<< "Please input blue grid coordinate:row column" <<endl;    cin>>bluerow>>bluecol;    Length= (1<<k);///long and wide are 2^k//dynamically allocated 2^k arrays chessboard=new int*[length];        for (int i=0;i<length;i++) {chessboard[i]=new int[length];    Initialized to-1 memset (chessboard[i],-1,length*sizeof (int)); } chessboard[bluerow][bluecol]=4;} VoID output (int **_chessboard) {for (int. i=0;i<length;i++) {for (int j=0;j<length;j++) {        cout<< "" <<_chessBoard[i][j];    } cout<<endl; } Cout<<endl;}        void Fillboard (int **_chessboard,int r,int c,int type) {for (int. i=0;i<2;i++) {for (int j=0;j<2;j++) {if ((i*2+j)!=type) {if (_chessboard[r+i][c+j]!=-1) COUT&LT;&L t; "                Error "<<endl;            _chessboard[r+i][c+j]=type; }}}}void fillchessboard (int **_chessboard,int level,int blue_row,int blue_col,int baserow,int baseCol) {if (        level==1) {int type= (blue_row<<1) +blue_col;    Fillboard (_chessboard,baserow,basecol,type);        }else {//otherwise four equal points, intermediate connections at the self-filling//new four-cell width int new_length=1<< (LEVEL-1);        int type= (blue_row/new_length) *2+blue_col/new_length; for (int. r=0;r<2;r++) {for (iNT c=0;c<2;c++) {if ((r*2+c) ==type) {Fillchessboard (_ches                SBOARD,LEVEL-1,BLUE_ROW-R*NEW_LENGTH,BLUE_COL-C*NEW_LENGTH,R*NEW_LENGTH+BASEROW,C*NEW_LENGTH+BASECOL); } else {Fillchessboard (_chessboard,level-1, (new_length-1) * (1-r), (New_le                NGTH-1) * (1-c), r*new_length+baserow,c*new_length+basecol);    }}} Fillboard (_chessboard,baserow+new_length-1,basecol+new_length-1,type); }}

Six, code interpretationThe program input is the chessboard size parameter k, the special lattice row and the column, the output is the entire chessboard, we use 4 to represent the special lattice, 0-3 respectively represents the 0-3 type L-type dominoes. The Fillboard () function is a function that overrides the chessboard with some kind of L-shaped domino. Fillchessboard () is a recursive solution to the entire problem of the function, input as a checkerboard pointer, the parameter level is K,blue_row,blue_col is the special lattice in the sub-chessboard coordinate system position, Baserow and Basecol is the sub-chessboard (0,0) Points in the coordinates of the entire large checkerboard.
Keep up with the new, stay tuned






The Board coverage problem of computer algorithm design and analysis

Related Article

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.