Chessboard coverage (java implementation)

6.2 chessboard coverage 1. Problem description ...
6.2 chessboard coverage

6.2 chessboard coverage

1. Problem description

In a 2k × In the chessboard composed of 2k squares, just one square is different from other squares. The square is called a special square and the chessboard is called a special chessboard. In the chessboard coverage problem, all squares on a given special chessboard except the special squares shall be covered with four different forms of L-shaped dominoes shown in the figure, and any two L-shaped dominoes shall not be overlapped.

2. Ideas

1. When k > 0, the 2k*2k chessboard is divided into four 2(k-1)*2(k-1) sub chessboards

2. Convert the three sub chessboards without special squares into special chessboards, and cover the meeting place of the three smaller chessboards with an L-shaped bone plate

3. Use this segmentation method recursively until the chessboard is simplified to 1 * 1 chessboard, and the recursion ends.

3. Attention

1. Judge the four small checkerboards after segmentation each time to judge whether the special square is inside.

The judgment method here is to record the row and column coordinates of the square in the upper left corner of the whole chessboard each time, and then compare with the coordinates of the special square to know whether the special square is in the chessboard.

  1. If the special box is inside, it can be solved directly recursively,
  2. If not, mark the squares in the lower right corner, lower left corner, upper right corner or upper left corner as special squares according to the different positions of the divided four chessboards, and then continue recursion. In the recursive function, there is also a variable s to record the number of squares on the edge. Each time the chessboard is divided, the number of squares on the edge will be halved. This variable is to facilitate the location of special squares.

The row and column coordinates of the square in the upper left corner are determined according to the size of the chessboard and which area to judge

Implementation details:

Take 4 * 4 chessboard as an example

To determine which area a special grid is in:

The chessboard can be divided into four regions according to the passed parameters size, leftrow and leftcol, and then compared with special points

1. top left corner:`specialRow < leftRow+size && specialCol < leftCol+size` 2. Upper right corner:`specialRow < leftRow+size && specialCol >= leftCol+size` 3. lower left quarter:`specialRow >= leftRow+size && specialCol < leftCol+size` 4. Lower right corner:`specialRow >= leftRow+size && specialCol >= leftCol+size`

The special points filled can also be determined by these parameters

If the special point is not in the upper left corner, the filled special point is in the lower right corner of the area leftRow+size-1,leftRol+size-1

If the special point is not in the upper right corner, the filled special point is in the lower left corner of the area leftRow+size-1,leftRol+size

If the special point is not in the lower left corner, the filled special point is in the upper right corner of the area leftRow+size,leftCol+size-1

If the special point is not in the lower right corner, the filled special point is in the lower left corner of the area leftRow+size,leftCol+size

2. End condition of recursion

  1. When size is 1
//When the size is 1, the recursion ends if (1 == size) { return; }
  1. When size is 2
//When the size is 2, the recursion ends if(size == 2){ number++; if(specialCol != leftCol || specialRow != leftRow) board[leftRow][leftCol] = number; if(specialCol != leftCol+1 || specialRow != leftRow+1) board[leftRow+1][leftCol+1] = number; if(specialCol != leftCol+1 || specialRow != leftRow) board[leftRow][leftCol+1] = number; if(specialCol != leftCol || specialRow != leftRow+1) board[leftRow+1][leftCol] = number; return; }

4. Code

package com.java.test; public class ChessProblem { int size;//capacity int[][] board;//checkerboard int specialRow;//Abscissa of special point static int number = 0;//L-shaped number, which must be static and can be accessed anywhere int specialCol;//Special point ordinate public ChessProblem(int specialRow, int specialCol, int size) { this.size = size; this.specialCol = specialCol; this.specialRow = specialRow; board = new int[size][size]; } //Row subscript of specialRow special point //Column subscript of specialCol special point //Left starting row subscript of leftRow matrix //The column subscript of the starting point on the left of the leftCol matrix //The width or height of a chessboard public void setBoard(int specialRow, int specialCol, int leftRow, int leftCol, int size) { //When the size is 1, the recursion ends if (1 == size) { return; } int subSize = size / 2; number++; int n = number;//Note that number must exist in the current recursive level, otherwise the recursive global variable will change when entering the next level //Suppose the special point is in the upper left corner if (specialRow < leftRow + subSize && specialCol < leftCol + subSize) { setBoard(specialRow, specialCol, leftRow, leftCol, subSize); } else { //Not in the upper left corner, let the lower right corner of the upper left corner matrix be the special point (place the L shape with others) board[leftRow + subSize - 1][leftCol + subSize - 1] = n; setBoard(leftRow + subSize - 1, leftCol + subSize - 1, leftRow, leftCol, subSize); } //Suppose the special point is at the top right if (specialRow < leftRow + subSize && specialCol >= leftCol + subSize) { setBoard(specialRow, specialCol, leftRow, leftCol + subSize, subSize); } else { //Not in the upper right, let the lower left corner of the upper right matrix be the special point (place the L shape with others) board[leftRow + subSize -1][leftCol + subSize] = n; setBoard(leftRow + subSize -1, leftCol + subSize, leftRow, leftCol + subSize, subSize); } //The special point is at the bottom left if (specialRow >= leftRow + subSize && specialCol < leftCol + subSize) { setBoard(specialRow, specialCol, leftRow + subSize, leftCol, subSize); } else { //Not in the lower left, let the upper right corner of the lower left matrix be the special point (place the L shape with others) board[leftRow + subSize][leftCol + subSize - 1] = n; setBoard(leftRow + subSize, leftCol + subSize - 1, leftRow + subSize, leftCol, subSize); } //The special point is in the lower right corner if (specialRow >= leftRow + subSize && specialCol >= leftCol + subSize) { setBoard(specialRow, specialCol, leftRow + subSize, leftCol + subSize, subSize); } else { //Not in the lower right corner, let the upper left of the lower right corner matrix be the special point (place the L shape with others) board[leftRow + subSize][leftCol + subSize] = n; setBoard(leftRow + subSize, leftCol + subSize, leftRow + subSize, leftCol + subSize, subSize); } } //Output chessboard public void printBoard(int specialRow,int specialCol,int size) { setBoard(specialRow, specialCol, 0, 0, size); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board.length; j++) { System.out.print(board[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { //Size of chessboard int N = 8; //Coordinates of special points int specialRow = 0; int specialCol = 1; ChessProblem chessProblem = new ChessProblem(specialRow , specialCol , N); chessProblem.printBoard(specialRow, specialCol, N); } }

5. Screenshot of operation

7 October 2021, 22:56 | Views: 2081

Add new comment

For adding a comment, please log in
or create account

0 comments