Board coverage (divide and conquer method)

Source: Internet
Author: User

Title: In a chessboard of 2^k x 2^k squares, if a square is different from the other squares, it is called a special square and is called a special chessboard.  Now use 4 different forms of L-shaped dominoes to cover all squares except special squares on a given special chessboard, and any 2 L-type dominoes must not overlap. Explain what is the L-type Domino: It is a corner of three squares, there are four kinds;

Idea: A chessboard is divided into four small chessboard, along the midpoint of each side of the split, special squares must be located in one of the 4 smaller chessboard, the remaining 3 sub-chessboard without special squares. In order to convert the 3 sub-chessboard with no special sub-chessboard into a special chessboard, we can use an L-shaped domino to cover the convergence of the 3 smaller chessboard, recursively go down, and when the length of the checkerboard is 1 o'clock terminated;

The code is as follows:

#include <iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<Set>#include<queue>#include<vector>using namespacestd;inttot;intboard[ the][ the];voidChessboard (intTrintTcintDrintdcintsize) {    if(size==1)return ; intt=tot++; ints=size/2; if(dr<tr+s&&dc<tc+s) chessboard (tr,tc,dr,dc,s); Else{board[tr+s-1][tc+s-1]=T; Chessboard (Tr,tc,tr+s-1, tc+s-1, s); }    if(dr<tr+s&&dc>=tc+s) Chessboard (TR,TC+s,dr,dc,s); Else{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 () {intdr,dc,s; printf ("the input format is: Special square horizontal axis, ordinate board edge length \ n");  while(SCANF ("%d%d%d", &dr,&dc,&s)! =EOF) {Tot=1; memset (board,0,sizeof(board)); Chessboard (0,0, dr,dc,s);  for(intI=0; i<s;i++)        {             for(intj=0; j<s;j++) printf ("%-2d", Board[i][j]); cout<<Endl; }    }    return 0;}

Run:

Board coverage (divide and conquer method)

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.