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 feel a bit ashamed. I have studied for one semester during the university. To now but still feel just the teacher talked about the topic to understand, and did not learn some of the algorithm of some good analytical methods and ideas, encountered a new problem often feel very tricky, after a bitter lesson to think or good to learn again. To be able to understand the ideas and core of each algorithm, at the same time also exhorts 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, just one square is different from the other squares. Called the box a special square, and called the Board
For a special chessboard. There are four kinds of L-type dominoes, for example, to cover all the squares except special squares with these four kinds of dominoes, and two L-type dominoes cannot cover each other.

Third, the problem solving ideas for complex problems. One of our often-used ideas 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 the problem of a 2*2 checkerboard. Because there are only four squares and a special lattice, it can only be covered with a corresponding L-shaped domino. The problem is very easy. here again we define four kinds of L-type dominoes:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahfondu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast "style=" font-size:18px ">

In the chessboard, we use (line). column) to represent a lattice, because (x, y) Such an expression is ambiguous for the image processing person, we prefer to think that the first dimension is a row, the second dimension is a column.

if it's in the 2*2 board. Special Check out today (0,0) This seat, then we want to use the 0-type dominoes cover the rest of the position, similarly, if the special lattice out of the position now (0,1), we have to use the 1-type dominoes to cover the rest of the position. More general. If the special lattice is present in this position of 2*2 (Row,col), we are going to use the Row*2+col Domino to cover the rest of the position, and both row and Col are indexed from 0.


When k=2, the problem becomes a 4*4 chessboard problem, this time the problem is slightly complicated, we think AH. Assuming 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, very fast change can solve, the remaining three? Let's draw a picture and take a good look.



If the special lattice is present (0,2) This position, 3 see, then for the special lattice of the upper right corner of the sub-chessboard we fill with 0 dominoes, 4. So the remaining three sub-chessboard, this time we found that the upper-left corner can only cover the 3 and 2, the other two will have the remaining space. If the 2-type dominoes are covered, the lower left corner must not be completely covered (you can try it), you can only use the Type 3 Domino overlay, and so on, we can also cover the lower left and lower right corner at this time only three squares are not covered, 5 see.

Now we have a detailed view of the remaining three squares, and we find that they are all 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 are not able to make this empty lattice as a special lattice, so that four sub-chessboard is a special lattice containing a small chessboard. So the original problem becomes four identical sub-problems, after solving each checkerboard, we then three false sub-checkerboard lattice cover (6).

So how to choose the space as a sub-chessboard of the special lattice it. By observing we find that we do not have to specify special lattices for the sub-chess with special lattices, for the remaining three sub-chessboard, we specify the lattice of the junction of the four sub-chessboard as a special lattice.

Iv. InductionNow we summarize our solution to the problem: first of all, the big chessboard is divided into four 2^ (k-1) *2^ (k-1) of the sub-chessboard. Then specify the position of the false special lattice for the sub-chessboard without special lattice. The original problem is decomposed into four sub-problems to solve. Of course, four sub-problems may not be solved directly, and there may be a continuation of the recursive solution. If four sub-problems have been solved, we should cover three false special squares with a specific L-shaped domino, and the whole big Board has been solved. We will think about the whole solution process, first of all to simplify the problem, simplify to see the answer directly to the point, and then analyze a slightly more complex situation, summarize the law,Use the idea of divided treatment. The problem is resolved into four sub-problems, respectively solving four sub-problems, in the process of solving sub-problems may also have sub-problems. Constant recursive solution, finally each sub-problem solved. Big problems are 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 interpretationprogram input for the board size of the parameter k, special lattice rows and columns, the output for the entire board, we use 4 for special lattice, 0-3 for the 0-3 type of L-type Domino.

The Fillboard () function is a function that overrides the chessboard with some kind of L-shaped domino. Fillchessboard () is a recursive function that solves the whole problem, and is entered as a checkerboard pointer. The participation level is also K. Blue_row,blue_col is the position of the special lattice in the coordinate system of the sub-chessboard, Baserow and Basecol are the coordinates of the sub-chessboard (0,0) points throughout the large checkerboard.


Keep up with the new. Please follow






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.