Use divide and conquer idea is simple chess program a cover in order to review the content of divide and conquer.
1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 #define n 4 7 int tile = 0; 8 9 int board[n][n]; 10 11 void ChessBoardCover(int tr, int tc, int dr, int dc, int size); 12 13 14 int main() 15 { 16 int a, b; 17 cout << "Input the position of the special check:" << endl; 18 cin >> a >> b; 19 20 ChessBoardCover(0,0,a,b,n); 21 22 cout << "Output the chessboard(0 represents the special one):" << endl; 23 for (int i = 0; i < n; i++) 24 { 25 for (int j = 0; j < n; j++) 26 cout << board[i][j] << " " ; 27 COUT << endl; 28 } 29 getchar (); 30 getchar (); 31 is } 32 33 is / * 34 is the tile is a global integer variable that represents the number of L-shaped dominoes, initial value 0. 35 TR: checkerboard squares line number of the upper left corner; 36 TC: checkerboard squares left corner column number; 37 [ DR: special line number where the box; 38 is DC: column number of special boxes located; 39 size: size = 2 ^ k, the board size of 2 × ^ K ^ 2 K 40 * / 41 is void ChessBoardCover ( int TR, int TC, int DR, int DC, int size) 42 is { 43 is IF (size == . 1 ) 44 is return ; 45 int T = the tile ++; // L-type domino No. 46 is int S = size / 2 ; // division board 47 48 @ covers the top left sub-board 49 IF ( DR <+ S && DC TR <TC + S) // special this checkerboard squares 50 ChessBoardCover (TR, TC, DR, DC, S); 51 is the else 52 is { // this is no special board box 53 is Board [TR S + - . 1 ] [S TC + - . 1 ] = T;// covered with the bottom right corner L-t domino No. 54 is ChessBoardCover (TR, TC, TR S + - . 1 , TC S + - . 1 , S); // covering the rest of the grid 55 } 56 is 57 is // cover the upper right corner sub-board 58 IF (DR <&& DC TR + S> = TC + S) // special this checkerboard squares 59 ChessBoardCover (TR, TC + S, DR, DC, S); 60 the else { // this is not particularly chessboard grid 61 is Board [S TR + - . 1 ] [S + TC] = t; // covered with the bottom left number L-domino t 62 @ covers the remaining box 63 ChessBoardCover (TR, TC + S, S TR + - . 1 , TC + S, S); 64 } 65 66 // cover the bottom left sub-board 67 IF (DR> = TR + DC && S <S + TC) // Special in this checkerboard squares 68 ChessBoardCover (TR + S, TC, DR, DC, S); 69 the else 70 { 71 is // covered with domino top right corner L-t No. 72 Board [TR + S] [S + TC - . 1 ] = T; 73 is 74 // cover remaining squares 75 ChessBoardCover (S + TR, TC, TR + S, S TC + - . 1 , S); 76 } 77 78 // cover the bottom right sub-board 79 IF (DR> S && DC = TR +> = TC + S) // special this checkerboard squares 80 ChessBoardCover (S + TR, TC + S, DR, DC , S); 81 the else 82 { 83 Board [TR + S] [S + TC] = t; // covering the top left number L-t-domino 84 ChessBoardCover (S + TR, TC + S, S + TR, TC S +, S); // covering the rest of the grid 85 } 86 }
Author:耑new New, released in blog Park
Please indicate the source, welcome message exchange: [email protected]