8 Queen Problem recursion implementation C language super detailed ideas basis, Queen Recursion

Source: Internet
Author: User

8 Queen Problem recursion implementation C language super detailed ideas basis, Queen Recursion

Queen eight's question: Assuming that eight queens are placed on the international chess board, they cannot attack each other. How many methods are there?

Basic knowledge:

In chess, the board is 8x8.

Each step of the queen can follow a straight line or a diagonal line.

Ideas:

1. If you want to put eight queens in, there will surely be only one queen in each row, and one queen in each column.

2. Set a two-dimensional array chess [I] [j] to simulate the chessboard. cas storage pendulum method. I j indicates the j column of the I row:

Write a function for recursion. The idea is as follows:

3. Move the queen from the top row to the bottom row, and place the queen from the leftmost (0th columns) when the row is put down. If not, move it to the right and try again. Check whether the right side has exceeded the chessboard.

4. Write a function to determine whether the current position can be placed. You only need to determine whether the current position is horizontal, vertical, and two diagonal lines. Whether there are any other queens on these four lines is enough. Name it check.

5. If the last row is complete, calculate the formula cas ++. Complete the last lineNoContinue to judge the next row.

6. After a situation is completed, you need to explore other situations. You can "Take Away" the current queen, and then test the board space that has not been tested before.

7. The operation to remove the queen can be implemented in the same code as the operation to remove the queen:

If this position cannot be placed, set it to zero, indicating that there is no queen.

If this position can be placed, put the Queen (set to 1 ). After the discussion, you have to remove it. The "Remove" operation is also set to zero.

Therefore, we should try to arrange the above Code to ensure that we have recorded the situations and then executed the code "Remove the Queen.

 

Below isRecursive functionsPart:

 

Void queen (int I, int j ){
If (j> = line) {// if the right side is out of bounds
Return;
}

If (check (I, j) = 1) {// if you can
Chess [I] [j] = 1; // Add the queen
If (I = line-1) {// record the status of the last row
Cas ++;
}
Else {
Queen (I +); // analyze the next row if it is not the last row
}
}
// The following two statements are the most brilliant.
Chess [I] [j] = 0; // if this position cannot be placed, set it to null (0) to determine the grid next to it.
// If this position can be put, it means that all the above code is executed, the Queen is taken away (set to zero), and other situations are discussed, and the next position is used for testing.
Queen (I, j + 1 );

}

Then begin to write the judgment Function check. You need to determine eight directions and consider it as four straight lines. For the horizontal column or vertical column, use the for loop to judge. Next, consider the diagonal line (red ).

 

In this way, we can see that the diagonal line to be determined is the split line of each quadrant.Change volumeIs equal, but there are differences in symbols. The range of X-axis and Y-axis variation is-8 ~ 8. Stop judgment when the diagonal line goes to the border.

Why is it-8 to 8? Because we do not need to determine the exact range of the diagonal line, it is the most ideal diagonal line, but because the destination location is different, the diagonal line range is also different, each calculation of the two ends of the point is not desirable.

Directly draw by the longest diagonal line:


 

Core code:

For (k =-line; k <= line; k ++) {// two diagonal lines
If (I + k> = 0 & I + k <line & j + k> = 0 & j + k <line) // diagonal line from top left to bottom right, if
If (chess [I + k] [j + k] = 1) return 0;

If (I-k> = 0 & I-k <line & j + k> = 0 & j + k <line) // diagonal line from bottom left to top right, if
If (chess [I-k] [j + k] = 1) return 0;
}

 

This judgment function is not important. You just need to write the function for the purpose.

Note that this function can only be designed as a judgment function and cannot be changed to fill the board space.

At the beginning, I thought that when I placed a queen, I directly filled all the numbers in the horizontal and vertical directions of the Queen, and listed them as "forbidden '', when placing the next queen, you only need to check whether the location is "forbidden. But it is wrong to do so, because it is okay not to fill the board space once. After setting it once, you need to take away the last piece of chess and discuss other situations. If, after the division, the queen has to restore the ban, it is very troublesome to say .. And the amount of code is not saved, so you just need to judge it.

 

The idea of the code block is complete. The following is the complete code. Running result: 92.

The program you write must also ensure this result.

 

# Include <stdio. h>
# Define line 8
Void queen (int I, int j );
Int check (int I, int j );

Int queennumber = 8;
Int chess [line] [line];
Int cas = 0;

Int main (){
Queen (0, 0 );
Printf ("% d \ n", cas );
Return 0;
}

Void queen (int I, int j ){
If (j> = line ){
Return;
}

If (check (I, j) = 1) {// if you can
Chess [I] [j] = 1; // Add the queen
If (I = line-1) {// record the status of the last row
Cas ++;
}
Else {
Queen (I +); // analyze the next row if it is not the last row
}
}
Chess [I] [j] = 0; // if this position cannot be placed, set it to null (0) to determine the grid next to it.
// If this position can be put, it means that all the above code is executed, the Queen is taken away (set to zero), and other situations are discussed, and the next position is used for testing.
Queen (I, j + 1 );
}


Int check (int I, int j ){
Int k;

For (k = 0; k <line; k ++ ){
If (chess [I] [k] = 1) return 0; // 0 = cannot be placed
}
For (k = 0; k <line; k ++ ){
If (chess [k] [j] = 1) return 0;
}
For (k =-line; k <= line; k ++) {// two diagonal lines
If (I + k> = 0 & I + k <line & j + k> = 0 & j + k <line) // diagonal line from top left to bottom right
If (chess [I + k] [j + k] = 1) return 0;

If (I-k> = 0 & I-k <line & j + k> = 0 & j + k <line) // diagonal line from bottom left to top right
If (chess [I-k] [j + k] = 1) return 0;
}
Return 1;
}

 

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.