Divide and conquer Method--board cover problem

Source: Internet
Author: User
divide and conquer method--board cover problem

Board coverage issues. There is a 2k∗2k 2^k*2^k checkered chessboard, just one square is black, the other is white. Your task is to cover all white squares with an L-shaped card containing 3 squares. Black squares cannot be overwritten, and any white squares cannot be overwritten by two or more cards at the same time. As shown in the figure is the L-type card 4 kinds of rotation.
Divide and conquer a three-step problem: Divide the chessboard of the 2k∗2k 2^k*2^k into 4 pieces of a sub-chessboard such as 2k−1∗2k−1 2^{k-1}*2^{k-1}. Recursive solution: Recursive fill each lattice, fill is divided into four cases, in the following will be explained, the recursive exit for K=0 k=0 that is, the number of sub-checkerboard squares is 1. Merge problem: You do not need to merge sub-issues. Four cases of recursive padding if the black box is on the upper-left sub-chessboard, the upper-left sub-chessboard is filled recursively, otherwise the lower-right corner of the upper-left sub-board is filled, and the lower-right corner is treated as a black square, and the upper-left sub-chessboard is recursively populated. If the black square is in the upper right sub-chessboard, the upper right sub-chessboard is filled recursively, otherwise the lower left corner of the upper right sub-board is filled, and the lower left corner is treated as a black square, and then the upper right sub-chessboard is filled recursively. If the black square is in the left-hand checkerboard, the left-bottom checkerboard is filled recursively, otherwise the upper-right corner of the checkerboard is filled, and the upper-right corner is treated as a black square, and then the left-bottom checkerboard is filled recursively. If the black box is in the right-hand checkerboard, the right-hand board is filled recursively, otherwise the lower-right corner of the checkerboard is filled, and the upper-left corner is treated as a black square, and then the right-hand checkerboard is filled recursively.

The algorithm of divide and conquer the board coverage problem

void chessboard (int row, int column, int x, int y, int siz) {//Recursive exit if (siz = = 1) {return;
    }//half divided into 2^ (siz-1) * 2^ (siz-1) of the chessboard int s = SIZ/2;
    L-type card number self-increment int t = ++number;
    The middle point, in which the sub-chessboard (x, y) is determined by which the int centerrow = row + S;
    int centercolumn = column + S;
    Black squares on the upper left sub-chessboard if (x < Centerrow && y < Centercolumn) {chessboard (row, column, X, y, s);
        } else {//does not fill the lower-right corner of the upper-left sub-chessboard chess[centerrow-1][centercolumn-1] = t;
    Then cover the other squares, noting that (x, y) is considered to be the filled position chessboard (row, column, CenterRow-1, centerColumn-1, s); }//Black squares on the upper right sub-chessboard if (x < centerrow && y >= centercolumn) {chessboard (row, Centercolumn, X, y
    , s);
        } else {//does not fill the lower-left corner of the upper-right sub-chessboard chess[centerrow-1][centercolumn] = t;
    Then cover the other squares, and notice that (x, y) is considered to be the filled position chessboard (row, Centercolumn, CenterRow-1, Centercolumn, s); }//Black squares in left-down checkerboard if (x >= centerrow && y < Centercolumn) {chessboard (centerrow, column, X, y, s);
        } else {//does not fill the upper-right corner of the checkerboard with chess[centerrow][centercolumn-1] = t;
    Then overwrite the other lattice, notice that (x, y) is considered to be filled position chessboard (centerrow, column, Centerrow, centerColumn-1, s); }//Black squares in right bottom checkerboard if (x >= centerrow && y >= centercolumn) {chessboard (Centerrow, Centercolum
    n, x, y, s);
        } else {//does not fill the upper left corner of the checkerboard chess[centerrow][centercolumn] = t;
    Then overwrite the other lattice, note that at this point (x, y) to be considered as filled position chessboard (Centerrow, Centercolumn, Centerrow, Centercolumn, s); }

}
Testing the main program
#include <iostream> using namespace std;
const int maxnum = 1 << 10;
Checkerboard Int Chess[maxnum][maxnum];

L-type card number int no.;
    void chessboard (int row, int column, int x, int y, int siz) {//Recursive exit if (siz = = 1) {return;
    }//half divided into 2^ (siz-1) * 2^ (siz-1) of the chessboard int s = SIZ/2;
    L-type card number self-increment int t = ++number;
    The middle point, in which the sub-chessboard (x, y) is determined by which the int centerrow = row + S;
    int centercolumn = column + S;
    Black squares on the upper left sub-chessboard if (x < Centerrow && y < Centercolumn) {chessboard (row, column, X, y, s);
        } else {//does not fill the lower-right corner of the upper-left sub-chessboard chess[centerrow-1][centercolumn-1] = t;
    Then cover the other squares, noting that (x, y) is considered to be the filled position chessboard (row, column, CenterRow-1, centerColumn-1, s); }//Black squares on the upper right sub-chessboard if (x < centerrow && y >= centercolumn) {chessboard (row, Centercolumn, X, y
    , s);
        } else {//does not fill the lower-left corner of the upper-right sub-chessboard chess[centerrow-1][centercolumn] = t; And then cover the other squares,Note that (x, y) is considered to be the filled position chessboard (row, Centercolumn, CenterRow-1, Centercolumn, s); }//Black squares in left-down checkerboard if (x >= centerrow && y < Centercolumn) {chessboard (centerrow, column, X, y
    , s);
        } else {//does not fill the upper-right corner of the checkerboard with chess[centerrow][centercolumn-1] = t;
    Then overwrite the other lattice, notice that (x, y) is considered to be filled position chessboard (centerrow, column, Centerrow, centerColumn-1, s); }//Black squares in right bottom checkerboard if (x >= centerrow && y >= centercolumn) {chessboard (Centerrow, Centercolum
    n, x, y, s);
        } else {//does not fill the upper left corner of the checkerboard chess[centerrow][centercolumn] = t;
    Then overwrite the other lattice, note that at this point (x, y) to be considered as filled position chessboard (Centerrow, Centercolumn, Centerrow, Centercolumn, s);
    }} int main () {//size, black square position int siz, x, y; while (true) {cout << (x, y) starts with (0,0), the input data is 0 0 0 to end the program.
        "<< Endl;
        cout << "Please enter the checkerboard size and black square position (x, y):";
        CIN >> siz >> x >> y; //exit Condition if (Siz = = 0) {break;

        }//Black (x, y), initialize L-shape Card No. chess[x][y] = number = 1;

        The divide-and-conquer method fills the checkerboard chessboard (0, 0, x, y, siz); Output the checkerboard for (int i = 0; i < siz; i++) {for (int j = 0; J < Siz; J + +) {cout <
            < chess[i][j] << "\ t";
        } cout << Endl << endl << Endl;
}} return 0; }
Output Data
(x, Y) starts with (0,0) and the input data is 0 0 0 which is the end of the program.
Please enter the checkerboard size and the black grid position (x, y): 2 0 0
1       2


2       2


(x, y) starting from (0,0), the input data is 0 0 0 which is the end program.
Please enter the checkerboard size and black square position (x, y): 4 1 1
3       3       4       4


3       1       2       4


5       2 2       6


5       5       6       6


(x, y) starting from (0,0), the input data is 0 0 0 to end the program.
Please enter the checkerboard size and black square position (x, y): 8 2 2
4       4       5       5       9       9       ten      4 3       3       5       9       8       8


6       3 1       7       8 6 6       7       7       2 each in      2       2      20 All in all, it      is



starting from (0,0), the input data is 0 0 (x, y) 0 is the end of the program.
Please enter the checkerboard size and black square position (x, y): 0 0 0

Process returned 0 (0x0)   execution time:29.988 s Press any
key to continue.

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.