The "N Queen problem" of the evolutionary process of the algorithm

Source: Internet
Author: User

The "N Queen problem" of the evolutionary process of the algorithm

Qiao is clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo )

The title comes from the play of chess, because the Queen's position can be vertical, horizontal, two oblique four-direction "capture", that is, there is no two queens or the same column, or on the same slash. And the N Queen question is how to decorate n Queens in the N*n chessboard so that there are no two queens in the same row and the same slash.

The best-known algorithm for solving the N-Queens problem is backtracking. Depending on the structure of the data and judging how the Queen can be placed in a certain position in the chessboard, we have a number of ways to implement the algorithm.

Method One:

Use a two-dimensional array Map[max][max] to store the checkerboard, initialize the board element value is 0, the Queen location element value is 1. After the display is finished, output the chessboard.

The algorithm uses recursive method to traverse every position in the chessboard, using the function place (introw, int col, int n) to determine whether the position of Map[row][col] can be placed on a pawn.

The code is as follows:

void queen_1 (int row, int n)//n Queen problem main recursive function {int col;if (row = = N)//all put {count++;p rintf ("%d disk: \ n", count); Print (n); return;} for (col=0; col<n; col++) {if (row, col, N)) {Map[row][col] = 1; Queen_1 (row+1, n); Map[row][col] = 0;}}} void Print (int n)//outputs a solution {int i, j;for (i=0; i<n; i++) {for (j=0; j<n; J + +) {if (map[i][j] = = 1) printf ("%c", 2); Elsepr intf ("#");} printf ("\ n");} printf ("\ n");} int place (int row, int col, int n)//determine Map[row][col] position can be placed pawn {int i;for (i=0; i<=row; i++) {if (map[i][col] = = 1)//Same column R Eturn 0;if (Col >= i && map[row-i][col-i] = = 1)//left Slash return 0;if (col+i<n && map[row-i][col+i] = = 1) Right slash return 0;} return 1;}

Method Two:

The place () function in method one is a common way to determine whether a pawn is placed in the current position, depending on the situation of the pendulum piece. In addition, you can modify the value of the position that the current piece can affect (my method is to increase the value by 2), indicating that the position can no longer be placed in the new pawn. To undo the current piece, then change the value of the corresponding position back.

The code is as follows:


void queen_2 (int row, int n)//n Queen problem main recursive function {int i, j;if (row = = N)//all put {count++;p rintf ("%d disk: \ n", count); Print (n); return;} for (i=0; i<n; i++) {if (map[row][i] = = 0) {Map[row][i] = 1;for (j=1; j<n-row; j + +)//Tag not placed sub {map[row+j][i] + = 2;if (i >= j)  //left slash map[row+j][i-j] + = 2;if (I+j < N)  //Right slash map[row+j][i+j] + = 2;} Queen_2 (row+1, N); for (j=1; j<n-row; j + +)//restore {map[row+j][i]-= 2;if (i >= J)  //left slash map[row+j][i-j]-= 2;if (I+j & Lt N)  //Right slash map[row+j][i+j]-= 2;} Map[row][i] = 0;}}}

Method Three:

The first two methods are using two-dimensional array to store the chessboard information, in fact, a one-dimensional array can also store the complete chessboard information. We set board[row] = col; the queen of row row is placed in Col.

The recursive backtracking algorithm is also used, but the place () function and the print () function are slightly different.

The code is as follows:

void Queen_3 (int row, int n)//n Queen problem main recursive function {int col, I, j;if (row = = N)//all put {count++;p rintf ("%d disk: \ n", count); Print_2 (n); return;} for (col=0; col<n; col++) {Board[row] = col;if (row) {place_2 (queen_3, n);}}} int place_2 (int row)//Determines whether the current game satisfies the condition {int i;for (i=0; i<row; i++) {if (board[i] = = Board[row])//The same column return 0;if (Board[i]  < Board[row] && (row-i) = = (Board[row]-board[i]))//left Slash return 0;if (Board[i] > Board[row] && (row-i) = = (Board[i]-board[row])//right slash return 0;} return 1;} void print_2 (int n)//outputs a solution {int i, j;for (i=0; i<n; i++) {for (j=0; j<n; J + +) {if (j = = Board[i]) printf ("%c", 2); elsep rintf ("#");} printf ("\ n");} printf ("\ n");}

Method Four:

There is a recursive approach to the backtracking algorithm, there is a corresponding non-recursive method, we can use the depth-first search of the general transformation method, the recursive method is converted to non-recursive mode. (for non-recursive conversions, I have compiled a lot of examples in my blog, interested users can go to Http://blog.csdn.net/qiaoruozhuo to see "non-recursive optimization" article classification)

The code is as follows:

void Queen_4 (int n)//n Queen problem Non-recursive function {int row, col, I, J;row = 0;board[0] = -1;while (row >= 0) {board[row]++; if (Row < n {if (Board[row] < n && place_2 (ROW))//initializes the next line board[++row] = -1;else if (board[row] = = N)//returns to the previous row row--;} else{count++;p rintf ("%d disk: \ n", count); Print_2 (n); row--;} }}

There are many algorithms to solve the problem of n Queens, and in another article, "The n Queen problem of optimization ", this paper introduces the method of using output symmetry graph, which is derived from method four. But only consider the case of the Queen of the first row on the left side of the chessboard (if n is an odd number, the first row of Queens is calculated separately), and a solution is obtained, and the symmetric solution is directly output, so that only half of the computational amount is required.

Others are using bit arithmetic to determine whether a position can be placed chess pieces, the efficiency is very high, we can go to study.



The "N Queen problem" of the evolutionary process of the algorithm

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.