Board cover (divide and conquer), Board cover divide and conquer

Source: Internet
Author: User

Board cover (divide and conquer), Board cover divide and conquer

Question: In a checker consisting of 2 ^ k x 2 ^ k squares, if there is a square different from other squares, it is called a special square, this Board is also called a special board. Now we need four different types of L-shaped bone cards to cover all squares except the special square on a given special board, and no two L-shaped bone cards can overlap. What is an L-shaped bone card? It is an angle consisting of three squares. There are four types;

Idea: divide a board into four small boards and split them along the midpoint of each side. The special Square must be located in one of the four smaller boards, there are no special squares in the remaining three sub-boards. In order to convert these three sub-boards without special sub-boards into special ones, we can use an L-type bone card to cover the three smaller ones and then recursively go down, terminate when the side length of the board is 1;

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 namespace std; int tot; int board [105] [105]; void chessboard (int tr, int tc, int dr, int dc, int size) {if (size = 1) return; int t = tot ++; int s = 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 (t R, 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 );}} int main () {int dr, dc, s; Printf ("input format: Special grid horizontal coordinates, ordinate Board side length \ n"); while (scanf ("% d", & dr, & dc, & s )! = EOF) {tot = 1; memset (board, 0, sizeof (board); chessboard (0, 0, dr, dc, s); for (int I = 0; I <s; I ++) {for (int j = 0; j <s; j ++) printf ("%-2d ", board [I] [j]); cout <endl ;}} return 0 ;}

 

Run:

 

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.