Chessboard coverage problem - Divide and Conquer

Recently a little bored I knocked on the chessboard coverage problem.

A: Algorithm Analysis

Chessboard checkerboard coverage problem in claim 2 ^ k * 2 ^ k consisting of squares, you given any particular point, to achieve full coverage with one embodiment of the board in addition to the specific point.

Model shown:


The solution is to use divide and conquer, the square chessboard divided into four parts, if at some point the special part, we went to the recursive him, if not a certain part, we assume a special point to point, the same recursion, knowing full coverage.

    The upper left corner of the sub-board (if there is a special box): the lower right corner of the box that the child is assumed to be a special board box;

    The upper right corner of the sub-board (if there is a special box): the lower left corner of the square of the sub board is assumed to be a special box;

    Lower left corner of the sub-board (if there is a special box): the upper right corner of the square of the sub board is assumed to be a special box;

    The lower right corner of the sub-board (if there is a special box): the upper left corner of the box that the child is assumed to be a special board box;

Code:

#include <the iostream>
the using namespace STD;
int Map [10] [10];
int = n-1; // 1 starts to fill the
void solve (int a, int b , int xl, int xr, int yl, int yr) {// parameters are passed to the special point coordinates x and y, x axis XL of the boundary xr, y-axis is the boundary yl to YR
IF (XL == == YR XR || yl) {
return; / / have all been charted out recursively
}
int XM = (XR + XL) / 2; // this is the boundary of the median axis x
int ym = (yl + yr) / 2; // y-axis position of the boundary number (these two parameters is used to determine the position of specific points on the board)

IF (a> B XM && <= YM) {// the upper right corner is black block
map [ym] [xm] = n; // upper right corner black block, the upper left corner to fill the intermediate position n, then this block is regarded as a black block area (special point), the black block belongs to the top left corner of the partition.
map [ym + 1] [xm ] = n; // the same reason, this will be the lower left corner of the black block, and belongs to the area of the lower left corner of the partition.
map [ym + 1] [xm + 1] = n; // this is the lower right corner, the reason supra.
++ n-;
Solve (a, b, XM +. 1, XR, YL, YM); // Note that the first and second parameters is where a and b, the area of the upper right corner of the partition recursively.
solve (xm + 1, ym + 1, xm + 1, xr, ym + 1, yr); // this is the lower right corner of the region partition recursively, note that the first and second parameters.
solve (xm, ym, xl, xm, yl, ym); // Here are the top left corner, and reason as above.
solve (xm, ym + 1, xl, xm, ym + 1, yr); // this is the lower left corner, the reason same as above.
}
IF (A <= B && XM> YM) {// the lower left corner is dark block
Map [YM] [XM] n-=;
Map [YM] [XM +. 1] = n-;
Map [YM +. 1] [XM +. 1] = n-;
n-++;
Solve (A, B, XL, XM, YM +. 1, YR);
Solve (XM, YM, XL, XM, YL, YM);
Solve (XM +. 1, YM, XM + . 1, XR, YL, YM);
Solve (+. 1 XM, YM +. 1, XM +. 1, XR, YM +. 1, YR);

}
if (a > xm && b > ym) {//右下角是黑块
map[ym][xm] = n;
map[ym][xm + 1] = n;
map[ym + 1][xm] = n;
n++;
solve(a, b, xm + 1, xr, ym+1, yr);
solve(xm, ym, xl, xm, yl, ym);
solve(xm + 1, ym, xm + 1, xr, yl, ym);
solve(xm, ym + 1, xl, xm, ym + 1, yr);
}
if (a <= xm && b <= ym) { //左上角是黑块
map[ym+1][xm+1] = n;
map[ym][xm + 1] = n;
map[ym + 1][xm] = n;
n++;
solve(a, b, xl, xm, yl, ym);
solve(xm + 1, ym + 1, xm + 1, xr, ym+1, yr);
solve(xm + 1, ym, xm + 1, xr, yl, ym);
solve(xm, ym + 1, xl, xm, ym + 1, yr);

}

}


main int () {
int A, B;

CIN >> A >> B;
position // is meant herein that the 8 * 8 checkerboard, then special points; solve (a, b, 0 , 7, 0, 7) as (A, B)
for (int I = 0; I <. 8; I ++) {
for (int J = 0; J <. 8; J ++) COUT << Map [I] [J] << "\ T";
<< endl COUT;
}

 

return 0;
}

Guess you like

Origin www.cnblogs.com/lfh123123/p/11761846.html