Board coverage issues (C + + implementation)

Source: Internet
Author: User

in a chessboard of 2kx2k squares, a square is different from other squares, calling it a special square and calling it a special chessboard. problem: Use 4 different forms of L-shaped dominoes to cover all squares except special squares on a given special chessboard, and any 2 must not overlap. There are 4k cases where special squares appear on the board. Thus, for any k>=0, there are 4k different special chessboard. It is easy to know that in any 2k * 2k chessboard, the number of L-type dominoes used is exactly (4k-1)/3.
    1. When k>0, divide the 2kx2k checkerboard into 4 2k-1x2k-1 sub-chessboard, as shown in figure (a).
    2. Special squares must be located in one of the 4 smaller sub-chessboard, with no special squares in the remaining 3 sub-chessboard.
    3. To convert the non-special square checkerboard to a special chessboard, a domino can be used to cover the 3 smaller checkerboard's rendezvous, as shown in figure (b), thus translating the original problem into 4 smaller checkerboard coverage issues.
    4. Use this division recursively until the chessboard is simplified to a checkerboard 1x1.
#include <iostream>using namespacestd;inttile=1;//number of L-type dominoes (increment)intboard[ -][ -];//Chess Board/****************************************************** Recursive way to implement the board overlay algorithm * Input parameters: * tr--The line number in the upper left corner of the current checkerboard * tc--the column number in the upper left corner of the current checkerboard * dr--the line number of the current special box * dc--the column number where the current special square is located * Size: The current chessboard: 2^k*****************************************************/voidChessboard (intTrintTcintDrintdcintsize) {    if(size==1)return; intt=tile++,s=size/2; if(Dr<tr+s && Dc<tc+s)///in the upper left corner areachessboard (tr,tc,dr,dc,s); Else///not in the upper-left corner{board[tr+s-1][tc+s-1]=t;///with the t number (denoted by a number) L-type dominoes cover the lower right cornerChessboard (tr,tc,tr+s-1, tc+s-1, s);///cover the remaining squares    }    if(Dr<tr+s && Dc>=tc+s)///in the upper right corner areaChessboard (tr,tc+s,dr,dc,s); Else///not in the upper-right corner of the area{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); }}intMain () {intsize; cout<<"Enter the size of the chessboard (the magnitude must be 2 n Power):"; CIN>>size; intindex_x,index_y; cout<<"enter the coordinates of the special grid position:"; CIN>>index_x>>index_y; Chessboard (0,0, index_x-1, index_y-1, size);  for(intI=0; i<size; i++ )     {          for(intj=0; j<size; J + +) cout<<Board[i][j]<<'\ t'; cout<<Endl; }}

Derivation process: The original equivalent to T (k) =4t (k-1) +1 recursion: 4T (k-1) =4 (4T (k-2) +1) =42t (k-2) +4 T (k) = 42T (k-2) +4 +1                       Also: 42T (k-2) =43t (k-3) +42 so T (k) = 43T (k-3) +42+4+1 ........... T (k) =4kt (0) +4k-1+...+4+1=o (4k)

Board coverage issues (C + + implementation)

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.